Hi all!
I want to measure the non-idle time spent in a given process. If I use Julia’s wall clock with time()
, other processes that run on my computer (like parallel threads) will change the measurement. Judging by the code sample below, @timed
seems to work, but I have not found another source. Can someone confirm this?
julia> using Base.Threads
julia> nthreads()
10
julia> function f() # dumb function
t = @elapsed for i in 1:100000
exp(rand())
end
return t
end;
julia> function g(N) # single-threaded loop
T = Vector{Float64}(undef, N)
for n in 1:N
T[n] = f()
end
return T
end;
julia> function h(N) # multi-threaded loop
T = Vector{Float64}(undef, N)
@threads for n in 1:N
T[n] = f()
end
return T
end;
julia> @timed g(1); @timed h(1); # compile once
julia> res_g = @timed g(nthreads());
julia> res_h = @timed h(nthreads());
julia> total_time_g = res_g.time;
julia> total_time_h = res_h.time;
julia> total_threads_time_g = sum(res_g.value);
julia> total_threads_time_h = sum(res_h.value);
julia> total_threads_time_g / total_time_g # around 1
0.9975217449895315
julia> total_threads_time_h / total_time_h # greater than 1
5.692441324049729