From d6199bfea699ae74f45af50c3045d832a79757e2 Mon Sep 17 00:00:00 2001 From: Hatem Nassrat Date: Wed, 17 Sep 2014 20:14:14 +0000 Subject: [PATCH] Adding loop test code. --- loop-test/loop-test.py | 33 ++++++++++++ loop-test/results/results.csv | 61 +++++++++++++++++++++ loop-test/tests.py | 99 +++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100755 loop-test/loop-test.py create mode 100644 loop-test/results/results.csv create mode 100644 loop-test/tests.py diff --git a/loop-test/loop-test.py b/loop-test/loop-test.py new file mode 100755 index 0000000..0aa6ff5 --- /dev/null +++ b/loop-test/loop-test.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import sys +import csv +import timeit + +MICRO = 10 ** 6 # how many microseconds in a sec + +def noop(*args): + pass + +def main(): + test_runs = 3 # 10 ** 3 + test_range = 10 + test_count = 30 + w = csv.writer(sys.stdout) + styles = ['map', 'list_comp', 'loop'] + w.writerow(['test', ''] + styles) + for t in range(6): + for i in range(test_range): + sr = {} + for style in styles: + tr = timeit.timeit( + "t()", + "from tests import test%(t)s as test_creator; t = test_creator('%(style)s', %(i)s)" % { 'style': style, 'i': (i+1)*test_count, 't': t+1 }, + number = test_runs, + ) + sr[style] = tr + w.writerow(['test%s'%(t+1), str(i+1)] + ['%.3f' % (sr[s] * MICRO / test_runs) for s in styles]) + sys.stdout.flush() + +if __name__ == '__main__': + main() diff --git a/loop-test/results/results.csv b/loop-test/results/results.csv new file mode 100644 index 0000000..29c8072 --- /dev/null +++ b/loop-test/results/results.csv @@ -0,0 +1,61 @@ +test,,map,list_comp,loop +test1,1,7.073,7.312,6.040 +test1,2,11.047,13.272,11.047 +test1,3,15.656,18.994,18.676 +test1,4,20.266,24.319,20.981 +test1,5,25.034,30.041,25.988 +test1,6,30.041,36.001,30.994 +test1,7,37.352,41.008,36.319 +test1,8,39.657,47.048,41.008 +test1,9,44.346,51.975,45.935 +test1,10,49.273,57.697,51.022 +test2,1,287.930,185.013,173.648 +test2,2,1090.686,706.355,662.645 +test2,3,2418.280,1580.636,1471.996 +test2,4,4273.335,2792.358,2616.962 +test2,5,6728.013,4393.339,4031.022 +test2,6,9556.055,6235.361,5791.028 +test2,7,12981.335,8519.729,7884.979 +test2,8,17022.292,11236.032,10465.701 +test2,9,22599.379,14330.705,12998.343 +test2,10,26715.040,17330.329,16053.677 +test3,1,10192.633,6268.978,5851.348 +test3,2,78685.045,50862.630,46212.355 +test3,3,262993.018,172087.669,156613.668 +test3,4,640459.379,403170.347,371235.609 +test3,5,1199440.002,783643.961,719916.344 +test3,6,2079129.060,1403111.935,1242798.646 +test3,7,3284796.317,2186232.011,1971901.019 +test3,8,4893622.398,3234438.976,2952066.024 +test3,9,6955078.999,4623496.294,4196753.661 +test3,10,9745362.282,6303009.033,5749389.966 +test4,1,6.994,7.629,10.729 +test4,2,11.047,13.351,18.279 +test4,3,15.736,18.994,26.385 +test4,4,20.663,24.716,34.650 +test4,5,24.954,29.961,42.995 +test4,6,30.041,36.319,51.339 +test4,7,34.332,41.326,59.366 +test4,8,39.339,46.968,67.711 +test4,9,46.651,52.611,75.340 +test4,10,49.035,58.015,83.367 +test5,1,285.069,185.966,269.731 +test5,2,1083.612,715.017,1042.684 +test5,3,2417.644,1590.331,2313.375 +test5,4,4258.951,2799.670,4119.396 +test5,5,6670.316,4387.299,6473.303 +test5,6,9569.327,6286.383,9232.283 +test5,7,12976.964,8535.385,12587.706 +test5,8,16931.375,11138.042,16382.694 +test5,9,21411.339,14088.710,20821.969 +test5,10,26506.344,17352.343,25577.386 +test6,1,9703.000,6453.991,8619.308 +test6,2,74633.042,51236.312,68336.964 +test6,3,248765.310,171385.050,230781.317 +test6,4,584289.312,407492.956,548589.309 +test6,5,1140658.696,795018.673,1079078.674 +test6,6,1970503.966,1414717.038,1884580.294 +test6,7,3117335.399,2173988.342,2917993.943 +test6,8,4658020.655,3324104.309,4449507.634 +test6,9,6628776.709,4704991.738,6361190.637 +test6,10,9101038.694,6487662.951,8635653.337 diff --git a/loop-test/tests.py b/loop-test/tests.py new file mode 100644 index 0000000..2fde474 --- /dev/null +++ b/loop-test/tests.py @@ -0,0 +1,99 @@ +def noop(*args): + pass + +def test1(style, count): + x = range(count) + if style == 'map': + def test(): + map(noop, x) + if style == 'list_comp': + def test(): + [noop(a) for a in x] + elif style == 'loop': + def test(): + for a in x: + noop(a) + return test + + +def test2(style, count): + x = range(count) + if style == 'map': + def test(): + map(lambda a: map(lambda b: noop(a,b), x), x) + if style == 'list_comp': + def test(): + [noop(a, b) for a in x for b in x] + elif style == 'loop': + def test(): + for a in x: + for b in x: + noop(a, b) + return test + +def test3(style, count): + x = range(count) + if style == 'map': + def test(): + map(lambda a: map(lambda b: map(lambda c: noop(a,b,c), x), x), x) + if style == 'list_comp': + def test(): + [noop(a, b, c) for a in x for b in x for c in x] + elif style == 'loop': + def test(): + for a in x: + for b in x: + for c in x: + noop(a, b, c) + return test + +def test4(style, count): + x = range(count) + if style == 'map': + def test(): + return map(noop, x) + if style == 'list_comp': + def test(): + return [noop(a) for a in x] + elif style == 'loop': + def test(): + r = [] + for a in x: + r.append(noop(a)) + return r + return test + +def test5(style, count): + x = range(count) + if style == 'map': + def test(): + return map(lambda a: map(lambda b: noop(a,b), x), x) + if style == 'list_comp': + def test(): + return [noop(a, b) for a in x for b in x] + elif style == 'loop': + def test(): + r = [] + for a in x: + for b in x: + r.append(noop(a, b)) + return r + return test + +def test6(style, count): + x = range(count) + if style == 'map': + def test(): + return map(lambda a: map(lambda b: map(lambda c: noop(a,b,c), x), x), x) + if style == 'list_comp': + def test(): + return [noop(a, b, c) for a in x for b in x for c in x] + elif style == 'loop': + def test(): + r = [] + for a in x: + for b in x: + for c in x: + r.append(noop(a, b, c)) + return r + return test