Skip to content

Instantly share code, notes, and snippets.

@lachlan-eagling
Created October 5, 2019 01:53
Show Gist options
  • Select an option

  • Save lachlan-eagling/012ecddaa8da8477fe9d5e6239692e50 to your computer and use it in GitHub Desktop.

Select an option

Save lachlan-eagling/012ecddaa8da8477fe9d5e6239692e50 to your computer and use it in GitHub Desktop.
Blog - Generators (Runtime Performance)
import cProfile
print("Generator Runtime Stats:")
cProfile.run('sum((i for i in range(100000)))')
print("List Runtime Stats:")
cProfile.run('sum([i for i in range(100000)])')
Generator Runtime Stats:
100005 function calls in 0.123 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
100001 0.015 0.000 0.015 0.000 <string>:1(<genexpr>)
1 0.000 0.000 0.123 0.123 <string>:1(<module>)
1 0.000 0.000 0.123 0.123 {built-in method builtins.exec}
1 0.108 0.108 0.122 0.122 {built-in method builtins.sum}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
List Runtime Stats:
5 function calls in 0.012 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.010 0.010 0.010 0.010 <string>:1(<listcomp>)
1 0.001 0.001 0.012 0.012 <string>:1(<module>)
1 0.000 0.000 0.012 0.012 {built-in method builtins.exec}
1 0.001 0.001 0.001 0.001 {built-in method builtins.sum}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment