from __future__ import print_function import sys import time print("Results from", sys.version.partition("\n")[0], "\n") print(" num using using") print(" ops '*' 'and'") fmt = "%6d %5.3f %5.3f" num_ops = 100 while num_ops < 100000: t1 = time.time() code = "def f(x):\n" code += "".join(" x * x\n" for i in range(num_ops)) compile(code, "" % num_ops, "exec") t2 = time.time() time_using_mult = t2-t1 t1 = time.time() code = "def g(x):\n" # The same performance occurs with 'or' instead of 'and' code += "".join(" x and x\n" for i in range(num_ops)) compile(code, "" % num_ops, "exec") t2 = time.time() time_using_and = t2-t1 print(fmt % (num_ops, time_using_mult, time_using_and)) num_ops *= 2