Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created April 4, 2025 07:43
Show Gist options
  • Save abhijitmamarde/2dc68634cf2c13e2f7d2f7ddd5598222 to your computer and use it in GitHub Desktop.
Save abhijitmamarde/2dc68634cf2c13e2f7d2f7ddd5598222 to your computer and use it in GitHub Desktop.
python script to evaluate performance
import time
def do_once():
s = 0
start = time.time()
for i in range(1_00_00_000):
s += i
print(f"Sum is {s:_}")
finish = time.time()
duration = finish - start
print(f"Required Time: {duration} seconds")
start = time.time()
for i in range(5):
do_once()
finish = time.time()
duration = finish - start
print(f"Total Required Time: {duration} seconds")
"""
$ python --version
Python 3.12.2
$ python -m cProfile check_time_perf.py
Sum is 49_999_995_000_000
Required Time: 0.5082433223724365 seconds
Sum is 49_999_995_000_000
Required Time: 0.5146310329437256 seconds
Sum is 49_999_995_000_000
Required Time: 0.4909186363220215 seconds
Sum is 49_999_995_000_000
Required Time: 0.5314862728118896 seconds
Sum is 49_999_995_000_000
Required Time: 0.5132949352264404 seconds
Total Required Time: 2.558673620223999 seconds
31 function calls in 2.560 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.560 2.560 {built-in method builtins.exec}
1 0.000 0.000 2.560 2.560 check_time_perf.py:1(<module>)
5 2.558 0.512 2.560 0.512 check_time_perf.py:3(do_once)
11 0.001 0.000 0.001 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
12 0.000 0.000 0.000 0.000 {built-in method time.time}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment