Guest User

N*k+N vs. N+1 calls for same generator expression

a guest
May 27th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. <redacted>$ python
  2. Python 2.7.17 (default, Apr 15 2020, 17:20:14)
  3. [GCC 7.5.0] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import random
  6. >>> import string
  7. >>> import cProfile
  8. >>>
  9. >>> def chrs(bits, len_string):
  10. ...     return ''.join(chr((bits >> (8*x)) & 0xff) for x in range(len_string))
  11. ...
  12. >>>
  13. >>> def bits_strings(num_strings, len_string):
  14. ...     return list(chrs(random.getrandbits(len_string*8), len_string) for x in range(num_strings))
  15. ...
  16. >>> cProfile.run('bits_strings(1000, 1000)')
  17.          2005004 function calls in 0.802 seconds
  18.  
  19.    Ordered by: standard name
  20.  
  21.    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  22.         1    0.000    0.000    0.803    0.803 <stdin>:1(bits_strings)
  23.      1000    0.001    0.000    0.799    0.001 <stdin>:1(chrs)
  24.   1001000    0.608    0.000    0.699    0.000 <stdin>:2(<genexpr>)
  25.         1    0.000    0.000    0.803    0.803 <string>:1(<module>)
  26.   1000000    0.091    0.000    0.091    0.000 {chr}
  27.         1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  28.      1000    0.003    0.000    0.003    0.000 {method 'getrandbits' of '_random.Random' objects}
  29.      1000    0.095    0.000    0.794    0.001 {method 'join' of 'str' objects}
  30.      1001    0.004    0.000    0.004    0.000 {range}
  31.  
  32.  
  33. >>> cProfile.run('bits_strings(1000, 1000)')
  34.          2005004 function calls in 0.760 seconds
  35.  
  36.    Ordered by: standard name
  37.  
  38.    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  39.         1    0.000    0.000    0.760    0.760 <stdin>:1(bits_strings)
  40.      1000    0.001    0.000    0.757    0.001 <stdin>:1(chrs)
  41.   1001000    0.579    0.000    0.663    0.000 <stdin>:2(<genexpr>)
  42.         1    0.000    0.000    0.761    0.761 <string>:1(<module>)
  43.   1000000    0.084    0.000    0.084    0.000 {chr}
  44.         1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  45.      1000    0.003    0.000    0.003    0.000 {method 'getrandbits' of '_random.Random' objects}
  46.      1000    0.088    0.000    0.751    0.001 {method 'join' of 'str' objects}
  47.      1001    0.004    0.000    0.004    0.000 {range}
  48.  
  49.  
  50. >>> def chrs(bits, len_string):
  51. ...     return ''.join(chr((bits >> (8*x)) & 0xff) for x in range(len_string))
  52. ...
  53. >>> def bits_strings(num_strings, len_string):
  54. ...     return list(chrs(random.getrandbits(len_string*8), len_string) for x in range(num_strings))
  55. ...
  56. >>> cProfile.run('bits_strings(1000, 1000)')
  57.          1005005 function calls in 0.181 seconds
  58.  
  59.    Ordered by: standard name
  60.  
  61.    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  62.         1    0.000    0.000    0.751    0.751 <stdin>:1(bits_strings)
  63.      1000    0.001    0.000    0.747    0.001 <stdin>:1(chrs)
  64.      1001    0.001    0.000    0.751    0.001 <stdin>:2(<genexpr>)
  65.         1    0.000    0.000    0.751    0.751 <string>:1(<module>)
  66.   1000000    0.085    0.000    0.085    0.000 {chr}
  67.         1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  68.      1000    0.003    0.000    0.003    0.000 {method 'getrandbits' of '_random.Random' objects}
  69.      1000    0.087    0.000    0.742    0.001 {method 'join' of 'str' objects}
  70.      1001    0.004    0.000    0.004    0.000 {range}
Advertisement
Add Comment
Please, Sign In to add comment