Skip to content

Instantly share code, notes, and snippets.

@StephanieSunshine
Last active February 9, 2022 14:39
Show Gist options
  • Save StephanieSunshine/674767323ad12b717b0d4db62c85a926 to your computer and use it in GitHub Desktop.
Save StephanieSunshine/674767323ad12b717b0d4db62c85a926 to your computer and use it in GitHub Desktop.
Python3 function benchmark tool
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
#!/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)
#!/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