Edit: I don’t understand why this was marked to close. This is not pseudo-code nor is it someone else,s code nor is it homework. Can I get some more concrete feedback?
I am beginning to learn Julia after using Matlab for several years. I started by implementing a simple polynomial multiplication (without FFT) to try and understand the role of type stability. However, I have the following timings which I can’t understand at all.
function cauchyproduct(L::Array{Float64},R::Array{Float64}) # good one for floats N = length(L) prodterm = zeros(1,2N-1) for n=1:N Lterm = view(L,1:n) Rterm = view(R,n:-1:1) prodterm[n] = dot(Lterm,Rterm) end for n = 1:N-1 Lterm = view(L,n+1:N) Rterm = view(R,N:-1:n+1) prodterm[N+n] = dot(Lterm,Rterm) end prodterm end function cauchyproduct(L,R) # bad one for testing N = length(L) prodterm = zeros(1,2N-1) for n=1:N Lterm = view(L,1:n) Rterm = view(R,n:-1:1) prodterm[n] = dot(Lterm,Rterm) end for n = 1:N-1 Lterm = view(L,n+1:N) Rterm = view(R,N:-1:n+1) prodterm[N+n] = dot(Lterm,Rterm) end prodterm end testLength = 10000 goodL = rand(1,testLength) goodR = rand(1,testLength) for j in 1:10 @time cauchyproduct(goodL,goodR) end @which cauchyproduct(goodL,goodR)
I haven’t even gotten to the point where I test the timing for the “bad” implementation because I get the following timings from 2 sequential runs of this code. In general, the timing I get per test can range between .05s to 2s. Typically, the timings for a single run through the for loop will all have similar timings (as in the example quoted), but even this isn’t always the case. Occasionally, I have it alternate such as .05s .05s 1.9s .04s .05s 2.1s etc etc.
Any idea why this is happening?
0.544795 seconds (131.08 k allocations: 5.812 MiB) 0.510395 seconds (120.00 k allocations: 5.340 MiB) 0.528362 seconds (120.00 k allocations: 5.340 MiB, 0.94% gc time) 0.507156 seconds (120.00 k allocations: 5.340 MiB) 0.507566 seconds (120.00 k allocations: 5.340 MiB) 0.507932 seconds (120.00 k allocations: 5.340 MiB) 0.527383 seconds (120.00 k allocations: 5.340 MiB) 0.513301 seconds (120.00 k allocations: 5.340 MiB, 0.83% gc time) 0.509347 seconds (120.00 k allocations: 5.340 MiB) 0.509177 seconds (120.00 k allocations: 5.340 MiB) 0.052247 seconds (120.00 k allocations: 5.340 MiB, 7.95% gc time) 0.049644 seconds (120.00 k allocations: 5.340 MiB) 0.047275 seconds (120.00 k allocations: 5.340 MiB) 0.049163 seconds (120.00 k allocations: 5.340 MiB) 0.049029 seconds (120.00 k allocations: 5.340 MiB) 0.054050 seconds (120.00 k allocations: 5.340 MiB, 8.36% gc time) 0.047010 seconds (120.00 k allocations: 5.340 MiB) 0.051240 seconds (120.00 k allocations: 5.340 MiB) 0.050961 seconds (120.00 k allocations: 5.340 MiB) 0.049841 seconds (120.00 k allocations: 5.340 MiB, 4.90% gc time)