Skip to content

Commit

Permalink
Add performance test for nested looping
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
timholy committed Aug 2, 2012
1 parent a6ad7ef commit 73d8ca4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/perf2/looping.jl
Original file line number Diff line number Diff line change
@@ -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

2 comments on commit 73d8ca4

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love these new performance tests.

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to the limits on specializing for tuple types. I will experiment with increasing or removing this limit and see if the cost is reasonable.

Please sign in to comment.