Created
December 18, 2012 19:07
-
-
Save anonymous/4330933 to your computer and use it in GitHub Desktop.
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 timeit | |
import subprocess | |
import shlex | |
import sys | |
import re | |
import table | |
def report(funcs, setup, stmt, verbose = True): | |
data = run_timeit(funcs, setup, stmt, verbose) | |
result = [] | |
for funcname, t, output in data: | |
output = output[output.find(':')+1:] | |
output = output.strip() | |
result.append((funcname, output)) | |
return table.table(result) | |
def reportmany(func_setup_stmts, verbose = True): | |
''' | |
reportmany( | |
[(func1,'import test as t','t.func1()'), | |
(func2,'import test as t','t.func2()'), | |
(func3,'import test as t','t.func3()')]) | |
''' | |
data = [] | |
for func, setup, stmt in func_setup_stmts: | |
if verbose: | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
funcname = func.func_name | |
cmd = stmt.format(f = funcname) | |
proc = subprocess.Popen( | |
shlex.split("python -mtimeit -s'{s}' '{c}'".format(s = setup, c = cmd)), | |
stdout = subprocess.PIPE) | |
output, err = proc.communicate() | |
output = output.rstrip() | |
t = parse_timeit(output) | |
data.append((funcname, t, output)) | |
data.sort(key = lambda t: t[1]) | |
if verbose: print | |
result = [] | |
for funcname, t, output in data: | |
output = output[output.find(':')+1:] | |
output = output.strip() | |
result.append((funcname, output)) | |
return table.table(result) | |
def parse_timeit(astr): | |
t = None | |
match = re.search('(\d[.\d]*) (\w+) per loop', astr) | |
if match: | |
t = float(match.group(1)) | |
unit = match.group(2) | |
if unit == 'msec': t *= 1000 | |
elif unit == 'sec': t *= 10**6 | |
if t: return t | |
else: raise ValueError('unparsable string {s!r}'.format(s = astr)) | |
def run_timeit(funcs, setup, stmt, verbose = True): | |
# I call a subprocess instead of using timeit.Timer because the subprocess | |
# chooses how many loops to run, instead of me having to code the logic. | |
data = [] | |
for func in funcs: | |
if verbose: | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
funcname = func.func_name | |
cmd = stmt.format(f = funcname) | |
# print(cmd) | |
proc = subprocess.Popen( | |
shlex.split("python -mtimeit -s'{s}' '{c}'".format(s = setup, c = cmd)), | |
stdout = subprocess.PIPE) | |
output, err = proc.communicate() | |
output = output.rstrip() | |
t = parse_timeit(output) | |
data.append((funcname, t, output)) | |
data.sort(key = lambda t: t[1]) | |
if verbose: print | |
return data | |
def timefn(funcname, *args, **kwargs): | |
''' | |
Source: http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator/1630626#1630626 | |
Author: http://stackoverflow.com/users/86643/denis | |
Usage: timefn( 'filter', arg1, arg2 ) | |
This is a more quiet version of timef. It does not print args,kwargs. | |
''' | |
fargs = '%s(*%s,**%s)'%(funcname, args, kwargs) | |
t = timeit.Timer( fargs, 'from __main__ import %s' % funcname ) | |
ntime = 3 | |
return t.timeit( ntime ) * 1e6 / ntime | |
def timef(funcname, *args, **kwargs): | |
''' | |
Source: http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator/1630626#1630626 | |
Author: http://stackoverflow.com/users/86643/denis | |
Usage: timef( 'filter', arg1, arg2 ) | |
''' | |
fargs = '{f}(*{a:r},**{k:r})'.format(f = funcname, a = args, k = kwargs) | |
n = timefn(funcname, *args, **kwargs) | |
print('%.0f usec %s' % (n, fargs)) | |
def timefq(funcname, *args, **kwargs): | |
''' | |
Source: http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator/1630626#1630626 | |
Author: http://stackoverflow.com/users/86643/denis | |
Usage: timefq( 'filter', arg1, arg2 ) | |
This is a more quiet version of timef. It does not print args,kwargs. | |
''' | |
n = timefn(funcname, *args, **kwargs) | |
print('%.0f usec %s' % (n, funcname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment