Posts

Showing posts with the label timeit

timeit example

An example of how to time a statement >>> import numpy as np >>> arr1 = np.random.randint(0,1600,(3000,3000)) >>> timeit arr1.argmin() 10 loops, best of 3: 34.2 ms per loop Tested under Mac Lion Os proc i7 and Python 2.7.1 r271:86832

Timeit function

Here i present a little function (with an example below) to time some bunch of code using timeit. # -*- coding: utf-8 *-* import timeit class Chronoit(): """ A little class to handle timeit output. The output is in seconds. """ def __init__(self, main_statement, setup_statement, total_iterations): self.main_stmt = main_statement self.setup_stmt = setup_statement self.total_iter = total_iterations t = timeit.Timer(self.main_stmt, self.setup_stmt) self.show_results(t.timeit(number=self.total_iter)) def show_results(self, time_elapsed): time_per_pass = time_elapsed / self.total_iter if time_per_pass > 60.0: minutes_per_pass = time_per_pass / 60.0 secs_per_pass = time_per_pass % 60.0 print "#" * 50 print "Timing" print "setup stmt: \"" + self.setup_stmt.split("\n")[1] + ...