import cProfile
import time
from numba import jit
@jit
def simulate(Input):
while_count = 10000000
while while_count:
while_count -= 1
Input += 1
for_count = 10000000
for i in range(for_count):
Input += 2
for i in range(for_count):
Input += 2
return Input
start = time.time()
simulate(100)
end = time.time()
print(end-start)
时间
0.31107425689697266
cProfile.run('simulate(100)')
4 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 0.py:5(simulate)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
未优化前
1.029198408126831
cProfile.run('simulate(100)')
4 function calls in 1.234 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 1.234 1.234 1.234 1.234 0.py:6(simulate)
1 0.000 0.000 1.234 1.234 <string>:1(<module>)
1 0.000 0.000 1.234 1.234 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}