Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 42 lines (34 sloc) 1.366 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
from timeit import Timer

class Benchmark(dict):
    """Benchmark a feature in different modules."""

    def __init__(self,modules,title='',runs=3,reps=1000):
        self.module_test = dict((m,'') for m in modules)
        self.runs = runs
        self.reps = reps
        self.title = title

    def __setitem__(self,module,(test_str,setup_str)):
        """Set the test code for modules."""
        if module == 'all':
            modules = self.module_test.keys()
        else:
            modules = [module]

        for m in modules:
            setup_str = 'import %s; import %s as np; ' % (m,m) \
                        + setup_str
            self.module_test[m] = Timer(test_str, setup_str)

    def run(self):
        """Run the benchmark on the different modules."""
        module_column_len = max(len(mod) for mod in self.module_test)

        if self.title:
            print self.title
        print 'Doing %d runs, each with %d reps.' % (self.runs,self.reps)
        print '-'*79

        for mod in sorted(self.module_test):
            modname = mod.ljust(module_column_len)
            try:
                print "%s: %s" % (modname, \
                    self.module_test[mod].repeat(self.runs,self.reps))
            except Exception, e:
                print "%s: Failed to benchmark (%s)." % (modname,e)

        print '-'*79
        print
Something went wrong with that request. Please try again.