Skip to content

Instantly share code, notes, and snippets.

@zed
Last active March 13, 2023 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/4276624 to your computer and use it in GitHub Desktop.
Save zed/4276624 to your computer and use it in GitHub Desktop.
/big.txt
/reporttime$py.class
#!/usr/bin/env python
import numpy as np
n = 50000
a = np.random.random_integers(low=0, high=1 << 10, size=2 * n).reshape(-1, 2)
np.savetxt('big.txt', np.c_[a, np.random.rand(n)], fmt='%i %i %s')
#!/usr/bin/env python
import random
import sys
from reporttime import get_functions_with_prefix, measure
try:
import numpy as np
def read_numpy_loadtxt(filename):
a = np.loadtxt(filename, dtype=[('label1', 'i'),
('label2', 'i'),
('edge', 'f')])
def read_numpy_loadtxt_float(filename):
a = np.loadtxt(filename)
def read_numpy_loadtxt_float_delimiter(filename):
a = np.loadtxt(filename, delimiter=' ')
except ImportError:
pass
def read_read(filename):
with open(filename, 'rb') as file:
data = file.read()
def read_readtxt(filename):
with open(filename, 'rU') as file:
text = file.read()
def read_readlines(filename):
with open(filename, 'rU') as file:
lines = file.readlines()
def read_james_otigo(filename):
"""
http://stackoverflow.com/q/13856319
"""
file = open(filename).readlines()
for line in file[1:]:
label1, label2, edge = line.strip().split()
def read_james_otigo_with_int_float(filename):
"""
http://stackoverflow.com/q/13856319
"""
file = open(filename).readlines()
for line in file[1:]:
label1, label2, edge = line.strip().split()
label1 = int(label1); label2 = int(label2); edge = float(edge)
def read_map(filename):
with open(filename) as file:
L = [(int(l1), int(l2), float(edge))
for line in file
for l1, l2, edge in [line.split()] if line.strip()]
def read_map_local(filename, _i=int, _f=float):
with open(filename) as file:
L = [(_i(l1), _i(l2), _f(edge))
for line in file
for l1, l2, edge in [line.split()] if line.strip()]
def read_map_float(filename):
with open(filename) as file:
L = [list(map(float, line.split()))
for line in file if line.strip()]
def main():
filename = sys.argv[1] if len(sys.argv) > 1 else 'big.txt'
funcs = get_functions_with_prefix('read_')
measure(funcs, args=[filename], comment=filename)
random.shuffle(funcs)
measure(funcs, args=[filename], comment=filename)
main()
# -*- coding: utf-8 -*-
import inspect
import sys
import timeit
from functools import partial, wraps
from timeit import default_timer as timer
__version__ = "0.4.0"
def measure_func(func, args, number=1):
"""Measure how long `func(*args)` takes in seconds."""
f = partial(func, *args) # pylint: disable=W0142
while True:
start = timer()
r = timeit.repeat(f, number=number, repeat=1)
if timer() - start > 1: # at least 1 second per measurement
break
number *= 2
return min(r + timeit.repeat(f, number=number, repeat=2)) / number
def accept_funcs(func):
"""Add function names if necessary.
Decorator for functions that accept either a sequence of functions
or (name, function) pairs.
"""
@wraps(func)
def wrapper(funcs, *args, **kwargs):
if hasattr(funcs[0], '__name__'):
funcs = [(f.__name__, f) for f in funcs]
return func(funcs, *args, **kwargs)
return wrapper
def human_seconds(seconds, fmt="%.3g %s"):
"""Return human-readable string that represents given seconds."""
t = 1e6 * seconds # start with µsec
for suff in "usec msec".split():
if t < 1000:
return fmt % (t, suff)
t /= 1000
return fmt % (t, " sec")
@accept_funcs
def measure(funcs, args, comment='', verbose=False, number=1):
"""Report how long `f(*args)` takes for each f in funcs."""
if not comment:
comment = repr(args)
# measure performance
results = []
w = max(len(name) for name, _ in funcs)
for name, f in funcs:
results.append((measure_func(f, args, number=number), name))
if verbose:
print("{:{}s} {:>9s} {}".format(
name, w, human_seconds(results[-1][0]), comment))
# print sorted results
results.sort()
mint = results[0][0] # minimal time
ratios = ["%5.2f" % (t / mint,) for t, _ in results]
maxratio_width = max(len(r) for r in ratios)
# header
print("{:{}s} {:>9s} {:>{}s} {}".format(
"name", w, "time", "ratio", maxratio_width, "comment"))
ratios = [s.rjust(maxratio_width) for s in ratios]
for (t, name), ratio in zip(results, ratios):
print("{:{}s} {:>9s} {} {}".format(
name, w, human_seconds(t), ratio, comment))
return results
def get_functions_with_prefix(prefix, module=None):
if module is None: # use caller's module
modname = inspect.currentframe().f_back.f_globals['__name__']
module = sys.modules[modname]
all_funcs = inspect.getmembers(module, inspect.isfunction)
return [f for name, f in all_funcs if name.startswith(prefix)]
import os
import re
from distutils.core import setup
classifiers = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Programming Language :: Python :: 2
Programming Language :: Python :: 3
Topic :: Software Development :: Libraries :: Python Modules
"""
name = 'reporttime'
here = os.path.dirname(os.path.abspath(__file__))
def read_version(filename):
with open(filename, 'rb') as file:
source = file.read().decode('utf-8')
return re.search(u'(?m)^__version__ = "([^"]+)"', source).group(1)
if __name__ == "__main__":
setup(
name=name,
version=read_version(os.path.join(here, name + '.py')),
description="Utility functions for benchmarking",
long_description="""Utility functions for benchmarking several implementations
of the same function""",
url="https://gist.github.com/4276624",
license='MIT',
classifiers=list(filter(len, classifiers.split('\n'))),
author="jfs",
author_email="isidore.john.r@gmail.com",
platforms='any',
py_modules=[name],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment