The following example exhibits performance that is counter-intuitive to me.
Why?
type fast
x
end
type slow
x :: Unsigned
end
function flog(y)
s = 0.0
for i=1:1000000
s += sqrt(y.x)
end
s
end
for i=1:3
@time flog(fast(42))
@time flog(slow(42))
end
Here is the output I get:
$ julia sq.jl
0.088505 seconds (2.00 M allocations: 30.519 MB, 38.67% gc time)
0.803340 seconds (3.00 M allocations: 45.784 MB, 3.86% gc time)
0.048212 seconds (2.00 M allocations: 30.518 MB, 4.71% gc time)
0.770123 seconds (3.00 M allocations: 45.776 MB, 0.25% gc time)
0.047239 seconds (2.00 M allocations: 30.518 MB, 2.01% gc time)
0.769541 seconds (3.00 M allocations: 45.776 MB, 0.25% gc time)
It shows that the "fast" structure makes the code run significantly faster.
That seems backwards. I was expecting that with the "Unsigned" type
assertion, the compiler might be able to deduce that sqrt will always
return a Float64, or at least narrow down the dispatch choices. But as the
times show, the type assertion makes the code run *much* slower. What's
going on?
Yes, I know if I declare x with a concrete type such as Int, the code will
do blazingly fast. But I was poking around to see if declaring it with an
abstract type narrower than Any would help.