#! /usr/bin/env python # -*- coding: utf-8 -*- """ Small Module to show the rapidity of a program. Credit: Lilian Besson for http://perso.crans.org/besson/cs101/solutions/examples/ License: GPLv3 """ import sys import atexit import time count = 0 def func(begintime=None): """Print the number of steps made from the beginning of the program. """ if begintime: endtime = time.time() print "Python v{version} used {diff:.4g} seconds for {count} computations of 2**1000.".format(version=sys.version[0:5], diff=endtime - begintime, count=count) else: print "At the end, count = {}".format(count) if __name__ == '__main__': atexit.register(func) print "Interupt that program as soon as possible. It will tell you how much empty iteration steps it have been able to do." begintime = time.time() try: while True: 2**1000 count += 1 except: func(begintime) finally: print "End."