Skip to content

Instantly share code, notes, and snippets.

@carlobaldassi
Last active Dec 18, 2015
Embed
What would you like to do?
Faster Enumerate and Zip, benchmarks code
# note: Enumerate2 and new_enumerate are `Enumerate` and `enumerate` in the pull request.
dosomething(x::Float64, i::Int) = x+i
function perf()
n = 1_000_000
const a = rand(n)
olden() = for (i, x) in enumerate(a) dosomething(x,i) end
function oldeninline()
e = Enumerate(a)
state = (1, start(e.itr))
while !done(e.itr, state[2])
v, s = next(e.itr, state[2])
(i, x), state = (state[1], v), (state[1]+1, s)
dosomething(x,i)
end
end
newen() = for (i, x) in new_enumerate(a) dosomething(x,i) end
function neweninline()
e = Enumerate2(a)
state = EnumState(1, start(e.itr))
while !done(e.itr, state.s)
v, s = next(e.itr, state.s)
(i, x), state = (state.i, v), EnumState(state.i+1, s)
dosomething(x,i)
end
end
manualen() = begin i=1; for x in a; i+=1; dosomething(x,1) end; end
compare([olden, oldeninline, newen, neweninline, manualen], 10)
end
dosomething(x, y) = x+y
dosomething(x, y, z) = x+y+z
function perf()
n = 10_000
const a = rand(n)
const b = randbool(n)
oldzip() = for (x, y) in zip(a, b) dosomething(x,y) end
function oldzipinline()
z = zip(a, b)
state = { start(itr) for itr in z.itrs }
while !done(z, state)
for i = 1:length(z.itrs)
z.vals[i], state[i] = next(z.itrs[i], state[i])
end
(x, y), state = tuple(z.vals...), state
dosomething(x, y)
end
end
newzip() = for (x, y) in new_zip(a, b) dosomething(x,y) end
function newzipinline()
z = new_zip(a,b)
state = (start(z.itrs[1]), start(z.itrs[2]),)
while !(done(z.itrs[1], state[1]) || done(z.itrs[2], state[2]))
v1,s1 = next(z.itrs[1], state[1])
v2,s2 = next(z.itrs[2], state[2])
(x,y),state = (v1,v2,),(s1,s2,)
dosomething(x, y)
end
end
function manualzip()
sa,sb = start(a),start(b)
while !(done(a,sa) || done(b,sb))
(x,sa),(y,sb) = next(a,sa), next(b,sb)
dosomething(x, y)
end
end
compare([oldzip, oldzipinline, newzip, newzipinline, manualzip], 100)
end
function perf3()
n = 10_000
const a = rand(n)
const b = randbool(n)
const c = rand(n)
oldzip() = for (x, y, z) in zip(a, b, c) dosomething(x,y,z) end
function oldzipinline()
zp = zip(a, b, c)
state = { start(itr) for itr in zp.itrs }
while !done(zp, state)
for i = 1:length(zp.itrs)
zp.vals[i], state[i] = next(zp.itrs[i], state[i])
end
(x, y, z), state = tuple(zp.vals...), state
end
end
newzip() = for (x, y, z) in new_zip(a, b, c) dosomething(x,y,z) end
function newzipinline()
zp = new_zip(a,b,c)
state = (start(zp.itrs[1]), start(zp.itrs[2]),start(zp.itrs[3]))
while !(done(zp.itrs[1], state[1]) || done(zp.itrs[2], state[2]) || done(zp.itrs[3], state[3]))
v1,s1 = next(zp.itrs[1], state[1])
v2,s2 = next(zp.itrs[2], state[2])
v3,s3 = next(zp.itrs[3], state[3])
(x,y,z),state = (v1,v2,v3),(s1,s2,s3)
dosomething(x, y, z)
end
end
function manualzip()
sa,sb,sc = start(a),start(b),start(c)
while !(done(a,sa) || done(b,sb) || done(c,sc))
(x,sa),(y,sb),(z,sc) = next(a,sa), next(b,sb), next(c,sc)
dosomething(x, y, z)
end
end
compare([oldzip, oldzipinline, newzip, newzipinline, manualzip], 100)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment