speed01.py
import time
def test01():
time01 = time.time()
str1 = ""
for i in range(1000000):
str1 += str(i)
time02 = time.time()
print("Zeit 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("Zeitaufwand für test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Ausführungsergebnis:
Zeit in test01: 1.515000s
Zeit in Test 02: 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("Zeit 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("Zeitaufwand für test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Ausführungsergebnis:
Zeit in test01: 0.970000s
Zeit in Test 02: 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_Erforderliche Zeit, wenn der Cache nicht verwendet wird:{:.6f}s".format(t2 - t1))
print("lru_Zeitaufwand für die Verwendung des Cache:{:.6f}s".format(t3 - t2))
Ausführungsergebnis:
lru_Erforderliche Zeit, wenn der Cache nicht verwendet wird: 3.699000s
lru_Erforderliche Zeit bei Verwendung des 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("Zeit 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("Zeitaufwand für test02:{:.6f}s".format(time02 - time01))
test01()
test02()
Ausführungsergebnis:
Zeit in test01: 1.631000s
Zeit in Test 02: 1.533000s
speed05.py
import time
def test01():
time01 = time.time()
lst = []
for i in range(10000000):
lst.append(i)
time02 = time.time()
print("Zeit in test01:{:.6f}s".format(time02 - time01))
def test02():
time01 = time.time()
lst = []
append = lst.append
for i in range(10000000):
append(i) # lst.Die Geschwindigkeit kann durch Anhängen anstelle von Anhängen verbessert werden
time02 = time.time()
print("Zeitaufwand für test02:{:.6f}s".format(time02 - time01))
def test03():
time01 = time.time()
lst = [i for i in range(10000000)]
time02 = time.time()
print("Zeitaufwand für test03:{:.6f}s".format(time02 - time01))
test01()
test02()
test03()
Ausführungsergebnis:
Zeit in test01: 1.023000s
Zeit in Test 02: 0.686000s
Zeit in Test 03: 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("Zeit 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("Zeitaufwand für test02:{:.6f}s".format(time02 - time01))
return sum
sum1 = test01(1, 100000000)
sum2 = test02(1, 100000000)
Ausführungsergebnis:
Zeit in test01: 5.534000s
Zeit in Test 02: 0.040000s
Die JIT-Compiler-Bibliothek Numba kann 100-mal schneller ausgeführt werden. Es scheint, dass Sie Leistung wie C-Sprache erhalten können
Sie müssen Numba installieren Wenn Python mit Anaconda installiert wird, scheint Numba von Anfang an enthalten zu sein.
Recommended Posts