Last active
February 9, 2022 14:39
-
-
Save StephanieSunshine/674767323ad12b717b0d4db62c85a926 to your computer and use it in GitHub Desktop.
Python3 function benchmark tool
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
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) |
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
#!/usr/bin/env python3 | |
from timer import Timer | |
def append_test(c): | |
a = [] | |
for n in range(0,c): | |
a.append(n) | |
def inplace_test(c): | |
a = [None] * c | |
for i, v in enumerate(a): | |
a[i] = i | |
count = 10000 | |
Timer.test("append", append_test, count) | |
Timer.test("inplace", inplace_test, count) |
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
#!/usr/bin/env python3 | |
# | |
# Wonderful function benchmark | |
# https://stackoverflow.com/questions/311775/create-a-list-with-initial-capacity-in-python | |
# | |
# | |
import time | |
class Timer(object): | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
end = time.time() | |
secs = end - self.start | |
msecs = secs * 1000 # millisecs | |
print('%fms' % msecs) | |
def test(name, fn, c): | |
print("%s: " % name, end="") | |
with Timer() as t: | |
fn(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment