#!/usr/bin/env python
import timeit
first_transformation = lambda x: x + 1
second_transformation = lambda x: x + 1
third_transformation = lambda x: x + 1
n = 1000
my_list = list(range(1000))
def timeit_args(f, args, *timeit_args, **timeit_kwargs):
def testf():
f(*args)
return timeit.timeit(testf, *timeit_args, **timeit_kwargs)
try:
xrange
except NameError:
xrange = range # Python 3
def one_pass(my_list):
for i in xrange(0, len(my_list)):
my_list[i] = first_transformation(my_list[i])
my_list[i] = second_transformation(my_list[i])
my_list[i] = third_transformation(my_list[i])
return my_list
print('one_pass: ' + str(timeit_args(one_pass, [my_list], number=n)))
def multi_pass(my_list):
range_end = len(my_list)
for i in xrange(0, range_end):
my_list[i] = first_transformation(my_list[i])
for i in xrange(0, range_end):
my_list[i] = second_transformation(my_list[i])
for i in xrange(0, range_end):
my_list[i] = third_transformation(my_list[i])
return my_list
print('multi_pass: ' + str(timeit_args(multi_pass, [my_list], number=n)))
def simple(my_list):
return [third_transformation(second_transformation(first_transformation(e))) for e in my_list]
print('simple: ' + str(timeit_args(simple, [my_list], number=n)))
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCmltcG9ydCB0aW1laXQKCmZpcnN0X3RyYW5zZm9ybWF0aW9uID0gbGFtYmRhIHg6IHggKyAxCnNlY29uZF90cmFuc2Zvcm1hdGlvbiA9IGxhbWJkYSB4OiB4ICsgMQp0aGlyZF90cmFuc2Zvcm1hdGlvbiA9IGxhbWJkYSB4OiB4ICsgMQoKbiA9IDEwMDAKbXlfbGlzdCA9IGxpc3QocmFuZ2UoMTAwMCkpCgpkZWYgdGltZWl0X2FyZ3MoZiwgYXJncywgKnRpbWVpdF9hcmdzLCAqKnRpbWVpdF9rd2FyZ3MpOgogICAgZGVmIHRlc3RmKCk6CiAgICAgICAgZigqYXJncykKICAgIHJldHVybiB0aW1laXQudGltZWl0KHRlc3RmLCAqdGltZWl0X2FyZ3MsICoqdGltZWl0X2t3YXJncykKCnRyeToKICAgIHhyYW5nZQpleGNlcHQgTmFtZUVycm9yOg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