speed01.py
import time
def test01():
time01 = time.time()
str1 = ""
for i in range(1000000):
str1 += str(i)
time02 = time.time()
print("Time taken in test01:{:.6f}s".format(time02 - time01))
def test02():
time01 = time.time()
lst1 = []
for i in range(1000000):
lst1.append(str(i))
str1 = "".join(lst1)
time02 = time.time()
print("Time taken in test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Execution result:
Time taken in test01: 1.515000s
Time taken in test02: 0.330000s
speed02.py
import time
def test01():
time01 = time.time()
n = 0
sum = 0
while (n < 10000000):
sum += n
n += 1
time02 = time.time()
print("Time taken in test01:{:.6f}s".format(time02 - time01))
def test02():
time01 = time.time()
sum = 0
for n in range(10000000):
sum += n
time02 = time.time()
print("Time taken in test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Execution result:
Time taken in test01: 0.970000s
Time taken in test02: 0.559000s
speed03.py
from functools import lru_cache
import time
def fibonacci_number1(n):
return n if n < 2 else fibonacci_number1(n - 1) + fibonacci_number1(n - 2)
@lru_cache()
def fibonacci_number2(n):
return n if n < 2 else fibonacci_number2(n - 1) + fibonacci_number2(n - 2)
t1 = time.time()
fibonacci_number1(35)
t2 = time.time()
fibonacci_number2(35)
t3 = time.time()
print("lru_Time required when not using cache:{:.6f}s".format(t2 - t1))
print("lru_Time required when using cache:{:.6f}s".format(t3 - t2))
Execution result:
lru_Time required when not using cache: 3.699000s
lru_Time required when using cache: 0.000000s
speed04.py
import time
from math import sqrt
def test01():
time01 = time.time()
for i in range(10000000):
sqrt(33)
time02 = time.time()
print("Time taken in test01:{:.6f}s".format(time02 - time01))
def test02():
time01 = time.time()
sqrt_in = sqrt
for i in range(10000000):
sqrt_in(33)
time02 = time.time()
print("Time taken in test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Execution result:
Time taken in test01: 1.631000s
Time taken in test02: 1.533000s
speed05.py
import time
def test01():
time01 = time.time()
lst = []
for i in range(10000000):
lst.append(i)
time02 = time.time()
print("Time taken in test01:{:.6f}s".format(time02 - time01))
def test02():
time01 = time.time()
lst = []
append = lst.append
for i in range(10000000):
append(i) # lst.Speed can be improved by using append instead of append
time02 = time.time()
print("Time taken in test02:{:.6f}s".format(time02 - time01))
def test03():
time01 = time.time()
lst = [i for i in range(10000000)]
time02 = time.time()
print("Time taken in test03:{:.6f}s".format(time02 - time01))
test01()
test02()
test03()
Execution result:
Time taken in test01: 1.023000s
Time taken in test02: 0.686000s
Time taken in test03: 0.542000s
speed06.py
import time
from numba import jit
def test01(x, y):
time01 = time.time()
sum = 0
for i in range(x, y):
sum += i
time02 = time.time()
print("Time taken in test01:{:.6f}s".format(time02 - time01))
return sum
@jit
def test02(x, y):
time01 = time.time()
sum = 0
for i in range(x, y):
sum += i
time02 = time.time()
print("Time taken in test02:{:.6f}s".format(time02 - time01))
return sum
sum1 = test01(1, 100000000)
sum2 = test02(1, 100000000)
Execution result:
Time taken in test01: 5.534000s
Time taken in test02: 0.040000s
JIT compiler library Numba can be executed 100 times faster, It seems that you can get performance like C language
You need to install Numba In addition, when python is installed with anaconda, it seems that Numba is included from the beginning.
Recommended Posts