Advertisement
sawczakl

Add 5 to each element

Feb 23rd, 2023 (edited)
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import timeit
  2. import random
  3. from pandas import Series
  4. from numpy import array
  5.  
  6. _L = [random.randint(1, 1_000) for _ in range(1_000)]
  7.  
  8. def _plus5(n):
  9.     return n + 5
  10.  
  11. def loop_build_add(L):
  12.     L2 = []
  13.     for elem in L:
  14.         L2 = L2 + [elem + 5]
  15.  
  16. def loop_build_increment(L):
  17.     L2 = []
  18.     for elem in L:
  19.         L2 += [elem + 5]
  20.  
  21. def loop_build_append(L):
  22.     L2 = []
  23.     for elem in L:
  24.         L2.append(elem + 5)
  25.  
  26. def loop_build_extend(L):
  27.     L2 = []
  28.     for elem in L:
  29.         L2.extend([elem + 5])
  30.  
  31. def loop_build_zeroes(L):
  32.     L2 = [0] * len(L)
  33.     for i in range(len(L)):
  34.         L2[i] = L[i] + 5
  35.  
  36. def loop_inplace_add(L):
  37.     for i in range(len(L)):
  38.         L[i] = L[i] + 5
  39.  
  40. def loop_inplace_increment(L):
  41.     for i in range(len(L)):
  42.         L[i] += 5
  43.  
  44. def map_gen(L):
  45.     map(_plus5, L)
  46.  
  47. def map_cast(L):
  48.     list(map(_plus5, L))
  49.  
  50. def listcomp_gen(L):
  51.     (elem + 5 for elem in L)
  52.  
  53. def listcomp_cast(L):
  54.     [elem + 5 for elem in L]
  55.  
  56. def pandas_series(L):
  57.     Series(L) + 5
  58.  
  59. def pandas_series_cast(L):
  60.     (Series(L) + 5).tolist()
  61.  
  62. def numpy_array(L):
  63.     array(L) + 5
  64.  
  65. def numpy_array_cast(L):
  66.     (array(L) + 5).tolist()
  67.  
  68. methods = [
  69.     # loop_build_add,       # takes obnoxiously long, about 1 order of magnitude more
  70.     loop_build_increment,
  71.     loop_build_append,
  72.     loop_build_extend,
  73.     loop_build_zeroes,
  74.     loop_inplace_add,
  75.     loop_inplace_increment,
  76.     map_gen,
  77.     map_cast,
  78.     listcomp_gen,
  79.     listcomp_cast,
  80.     pandas_series,
  81.     pandas_series_cast,
  82.     numpy_array,
  83.     numpy_array_cast
  84. ]
  85.  
  86. for cb in methods:
  87.     result = timeit.timeit(lambda: cb(_L[:]), number=10_000)
  88.     print(f'{cb.__name__: <25}{result:.5f}')
  89.  
  90. """
  91. loop_build_add           7.60634
  92. loop_build_increment     0.77225
  93. loop_build_append        0.43777
  94. loop_build_extend        0.66135
  95. loop_build_zeroes        0.48419
  96. loop_inplace_add         0.44560
  97. loop_inplace_increment   0.51993
  98. map_gen                  0.01627
  99. map_cast                 0.61088
  100. listcomp_gen             0.01621
  101. listcomp_cast            0.29445
  102. pandas_series            2.16272
  103. pandas_series_cast       2.19919
  104. numpy_array              0.34844
  105. numpy_array_cast         0.43979
  106. """
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement