#!/usr/bin/python2 import profile N = 100000 L = range(0, N) # = [0, 1, 2, 3, ... N-3, N-2, N-1] # now, four ways to create a new list, such that each element is twice # the corresponding element in L. [0, 1, 2, 3, ...] -> [0, 2, 4, 6, ...] def test_concat_loop(): L2 = [] for n in L: L2 = L2 + [n*2] def test_append_loop(): L2 = [] for n in L: L2.append(n*2) def test_def_map(): def tmpfunc(arg): return arg * 2 L2 = map(tmpfunc, L) def test_lambda_map(): L2 = map(lambda n: n*2, L) def test_comp(): L2 = [ n*2 for n in L ] def test(): # the concatenation method is an order of magnitude slower than # the next best method #test_concat_loop() test_append_loop() test_def_map() test_lambda_map() test_comp() # note: this is for an EASY operation, one that does not require a function # call. I'm pretty sure the function call is what's killing the map/def and # map/lambda methods. If you NEED a function call, then they're not so bad. profile.run('test()')