From 73d8ca4eb63f0cac370c67c68de352e60de9c990 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 2 Aug 2012 06:12:22 -0500 Subject: [PATCH] Add performance test for nested looping This is extremely curious: in a nest of 3 loops, I get a huge performance gain (10x) by moving the inner loop into a separate function. If I had to speculate wildly, I suspect this has something to do with the compiler worrying that the outer loop variables might be changed by the inner loop? And that putting it inside a function removes that concern? --- test/perf2/looping.jl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/perf2/looping.jl diff --git a/test/perf2/looping.jl b/test/perf2/looping.jl new file mode 100644 index 0000000000000..cab42a70c9e86 --- /dev/null +++ b/test/perf2/looping.jl @@ -0,0 +1,40 @@ +function testlooping1(p, sz) + index = 1 + for k = 1:sz[3] + for j = 1:sz[2] + jkprod = j*k + for i = 1:sz[1] + p[index] = i*jkprod + index += 1 + end + end + end +end + +function oneloop(p, index::Int, jkprod::Int, imax::Int) + for i = 1:imax + p[index] = i*jkprod + index += 1 + end + return index +end + +function testlooping2(p, sz) + index = 1 + for k = 1:sz[3] + for j = 1:sz[2] + index = oneloop(p, index, j*k, sz[1]) + end + end +end + +sz = (1000,1000,10) +p = Array(Int, sz) +szsmall = (10,10,10) +psmall = Array(Int, szsmall) +testlooping1(psmall, szsmall) +@time testlooping1(p, sz) +p1 = copy(p) +testlooping2(psmall, szsmall) +@time testlooping2(p, sz) +@assert p == p1