Regarding speeding up python (memo)

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

Recommended Posts

Regarding speeding up python (memo)
Python memo
python memo
Python memo
Python memo
Python memo
Python memo
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)
Visualization memo by Python
Python test package memo
[Python] Memo about functions
python regular expression memo
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
TensorFlow API memo (Python)
python useful memo links
Python basic memo --Part 1
Effective Python Memo Item 3
Divisor enumeration Python memo
Tips for speeding up python code correctly with numba
A note on speeding up Python code with Numba
Reading, displaying and speeding up gifs with python [OpenCV]
Memo of "Cython-Speeding up Python by fusing with C"
Set up Python 3.4 on Ubuntu
Python memo (for myself): Array
Python execution time measurement memo
Twitter graphing memo with Python
Set Up for Mac (Python)
[Line / Python] Beacon implementation memo
Python and ruby slice memo
Python Basic Grammar Memo (Part 1)
Python code memo for yourself
Raspberry Pi + Python + OpenGL memo
Python basic grammar (miscellaneous) Memo (3)
Python immutable type int memo
Python memo using perl --join
Python data type summary memo
Python basic grammar (miscellaneous) Memo (2)
[MEMO] [Development environment construction] Python