Last active
March 29, 2022 04:30
-
-
Save ivanleoncz/a0947ea3a12b957de33f47d733ebabec to your computer and use it in GitHub Desktop.
Timeit and Understanding how fast different approaches can be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 -m timeit "'foo' + 'bar'" | |
50000000 loops, best of 5: 5.31 nsec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 -m timeit -r 10 "'foo' + 'bar'" | |
50000000 loops, best of 10: 5.29 nsec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 -m timeit -r 10 -n 100000 "'foo' + 'bar'" | |
100000 loops, best of 10: 5.28 nsec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 -m timeit -r 10 -n 100000 "''.join(('foo', 'bar'))" | |
100000 loops, best of 10: 42.5 nsec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 -m timeit -r 10 -n 100000 "'%s, %s' % ('foo', 'bar')" | |
100000 loops, best of 10: 63.1 nsec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_str_1() -> str: | |
return 'foo' + 'bar' | |
def test_str_2() -> str: | |
return ''.join(('foo', 'bar')) | |
def test_str_3() -> str: | |
return '%s, %s' % ('foo', 'bar') | |
if __name__ == "__main__": | |
import timeit | |
r = 10 | |
n = 100000 | |
print("test_str_1: ", timeit.repeat(setup="from __main__ import test_str_1", stmt="test_str_1()", number=n, repeat=r)) | |
print("test_str_2: ", timeit.repeat(setup="from __main__ import test_str_2", stmt="test_str_2()", number=n, repeat=r)) | |
print("test_str_3: ", timeit.repeat(setup="from __main__ import test_str_3", stmt="test_str_3()", number=n, repeat=r)) | |
# $ python3 ex_6.py | |
# test_str_1: [0.003694088023621589, 0.0037375990068539977, 0.003711616969667375, 0.0036614019772969186, 0.0036590969539247453, 0.003655606007669121, 0.003692480968311429, 0.0037061700131744146, 0.0036806080024689436, 0.003687592048663646] | |
# test_str_2: [0.008561848953831941, 0.008489075000397861, 0.00847748201340437, 0.00858063600026071, 0.008465468999929726, 0.008496129012200981, 0.008449406013824046, 0.008478877949528396, 0.008490820997394621, 0.008434668998233974] | |
# test_str_3: [0.013133066997397691, 0.013086761988233775, 0.013070349988993257, 0.013110997970215976, 0.01309297897387296, 0.013118749018758535, 0.013096819049678743, 0.013113581982906908, 0.013256893958896399, 0.013094236026518047] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_str_1() -> str: | |
return 'foo' + 'bar' | |
def test_str_2() -> str: | |
return ''.join(('foo', 'bar')) | |
def test_str_3() -> str: | |
return '%s, %s' % ('foo', 'bar') | |
if __name__ == "__main__": | |
import matplotlib.pyplot as plt | |
import timeit | |
r = 10 | |
n = 1000000 | |
test_str_1 = timeit.repeat(setup="from __main__ import test_str_1", stmt="test_str_1()", number=n, repeat=r) | |
test_str_2 = timeit.repeat(setup="from __main__ import test_str_2", stmt="test_str_2()", number=n, repeat=r) | |
test_str_3 = timeit.repeat(setup="from __main__ import test_str_3", stmt="test_str_3()", number=n, repeat=r) | |
f = plt.figure() | |
f.set_figwidth(10) | |
f.set_figheight(5) | |
plt.plot(test_str_1, marker='o', label="sum") | |
plt.plot(test_str_2, marker='o', label="join") | |
plt.plot(test_str_3, marker='o', label="% interpolation") | |
plt.title(f"List Extension Methods and Best Times from {n} Executions in {r} Rounds") | |
plt.xlabel("Rounds") | |
plt.ylabel("Units of Seconds") | |
plt.xticks(range(0, r)) | |
plt.legend() | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_concat_1(): | |
l = ["a", "b", "c"] | |
return l + ["d", "e", "f"] | |
def list_concat_2(): | |
l = ["a", "b", "c"] | |
l += ["d", "e", "f"] | |
return l | |
def list_concat_3(): | |
l = ["a", "b", "c"] | |
l.extend(["d", "e", "f"]) | |
return l | |
if __name__ == "__main__": | |
import timeit | |
r = 10 | |
n = 100000 | |
print("list_concat_1: ", timeit.repeat(setup="from __main__ import list_concat_1", stmt="list_concat_1()", number=n, repeat=r)) | |
print("list_concat_2: ", timeit.repeat(setup="from __main__ import list_concat_2", stmt="list_concat_2()", number=n, repeat=r)) | |
print("list_concat_3: ", timeit.repeat(setup="from __main__ import list_concat_3", stmt="list_concat_3()", number=n, repeat=r)) | |
# $ python3 ex_7.py | |
# list_concat_1: [0.013129231985658407, 0.013070706045255065, 0.012952743971254677, 0.012848541024141014, 0.012713467003777623, 0.012749644985888153, 0.012814807007089257, 0.0128303820383735, 0.012878363020718098, 0.012793435947969556] | |
# list_concat_2: [0.01266855897847563, 0.01276836299803108, 0.012766197032760829, 0.013836722995620221, 0.012670096999499947, 0.012525454978458583, 0.0126781280268915, 0.01254989899462089, 0.012621487025171518, 0.012537747039459646] | |
# list_concat_3: [0.01478160498663783, 0.016013532993383706, 0.015955634997226298, 0.014748011017218232, 0.014714905992150307, 0.01476882299175486, 0.015143102034926414, 0.015066416002810001, 0.014712321979459375, 0.014758766046725214] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_concat_1(): | |
l = ["a", "b", "c"] | |
return l + ["d", "e", "f"] | |
def list_concat_2(): | |
l = ["a", "b", "c"] | |
l += ["d", "e", "f"] | |
return l | |
def list_concat_3(): | |
l = ["a", "b", "c"] | |
l.extend(["d", "e", "f"]) | |
return l | |
if __name__ == "__main__": | |
import matplotlib.pyplot as plt | |
import timeit | |
r = 10 | |
n = 1000000 | |
list_concat_1 = timeit.repeat(setup="from __main__ import list_concat_1", stmt="list_concat_1()", number=n, repeat=r) | |
list_concat_2 = timeit.repeat(setup="from __main__ import list_concat_2", stmt="list_concat_2()", number=n, repeat=r) | |
list_concat_3 = timeit.repeat(setup="from __main__ import list_concat_3", stmt="list_concat_3()", number=n, repeat=r) | |
f = plt.figure() | |
f.set_figwidth(10) | |
f.set_figheight(5) | |
plt.plot(list_concat_1, marker='o', label="sum") | |
plt.plot(list_concat_2, marker='o', label="incremented") | |
plt.plot(list_concat_3, marker='o', label="extended") | |
plt.title(f"List Extension Methods and Best Times from {n} Executions in {r} Rounds") | |
plt.xlabel("Rounds") | |
plt.ylabel("Nanoseconds (nsecs)") | |
plt.xticks(range(0, r)) | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment