pointless musings on performance

mk mrkafk at gmail.com
Tue Nov 24 07:03:44 EST 2009


#!/usr/local/bin/python

import timeit


def pythonic():
     nonevar = None
     zerovar = 0
     for x in range(1000000):
         if nonevar:
             pass
         if zerovar:
             pass

def unpythonic():
     nonevar = None
     zerovar = 0
     for x in range(1000000):
         if nonevar is not None:
             pass
         if zerovar > 0:
             pass

for f in [pythonic, unpythonic]:
     print f.func_name, timeit.timeit(f, number=10)



# ./t.py
pythonic 2.13092803955
unpythonic 2.82064604759

Decidedly counterintuitive: are there special optimizations for "if 
nonevar:" type of statements in cpython implementation?


regards,
mk





More information about the Python-list mailing list