1 | from math import sqrt |
---|

2 | from sys import stdout as out, version_info |
---|

3 | from timeit import timeit |
---|

4 | |
---|

5 | def tester(tests, inits): |
---|

6 | fmt = '%%-%ds %%s\n' % ( |
---|

7 | 6+max(len(t+i) for t in tests for i in inits), ) |
---|

8 | for test in tests: |
---|

9 | cmd = 'if %s: pass' % test |
---|

10 | for setup in inits: |
---|

11 | header = 'if %s {%s}' % (test, setup) |
---|

12 | try: |
---|

13 | trials = [ timeit(cmd, setup, number=20000000) |
---|

14 | for i in range(3) ] |
---|

15 | mean = sum(trials) / float(len(trials)) |
---|

16 | stdev = sqrt( sum(x**2 for x in trials)/float(len(trials)) |
---|

17 | - mean**2 ) |
---|

18 | out.write( |
---|

19 | fmt % ( header, "%0.6f +/- %0.3f" % (mean, stdev) ) ) |
---|

20 | except KeyboardInterrupt: |
---|

21 | raise SystemExit |
---|

22 | except: |
---|

23 | out.write( fmt % (header, '(exception)') ) |
---|

24 | out.flush() |
---|

25 | out.write('\n') |
---|

26 | |
---|

27 | out.write('='*72+"\nTesting on Python %s\n\n" % ( |
---|

28 | '.'.join(str(x) for x in version_info[:3]),) ) |
---|

29 | |
---|

30 | tester(['a == True','a == False','a is True','a is False', 'a', 'not a'], |
---|

31 | ['a = True','a = False', 'a = 1', 'a = 0']) |
---|