Consider a toy algorithm that finds the maximum of a vector, then removes it and alters the other entries of the vector in a way that depends on whether they are located at a lower or higher index.
In the code below, why is the function foo()
so much slower than baz()
?
function foo(a::Vector)
for j in 1:length(a)
b, k = findmax(a)
for i in 1:k-1
a[i] = (a[i] + b) / 2
end
for i in k+1:length(a)
a[i] = (a[i] + 1) / 2
end
deleteat!(a, k)
end
end
function bar(a::Vector)
for j in 1:length(a)
b, k = findmax(a)
deleteat!(a, k)
a[:] = collect(
i < k ? (a[i] + b) / 2 : (a[i] + 1) / 2
for (i, c) in enumerate(a)
)
end
end
function baz(m::Int)
a = rand(m)
t_foo = @time foo(a) # 10.415122 seconds
t_bar = @time bar(a) # 0.000000 seconds
end
baz(100000)
I expected foo()
to be faster, because it works individually on the indices as they come up, whereas baz()
has to collect an entire new vector.