# Measuring non-idle time spent in process

**URL:** https://discourse.julialang.org/t/measuring-non-idle-time-spent-in-process/86832
**Category:** General Usage
**Tags:** parallel, multithreading, benchmark, time
**Created:** [September 6, 2022, 8:07am UTC](https://discourse.julialang.org/t/measuring-non-idle-time-spent-in-process/86832 "2022-09-06T08:07:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 6, 2022, 8:07am UTC](https://discourse.julialang.org/t/measuring-non-idle-time-spent-in-process/86832/1 "2022-09-06T08:07:13Z")

</div>

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
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

```

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 6, 2022, 8:10am UTC](https://discourse.julialang.org/t/measuring-non-idle-time-spent-in-process/86832/2 "2022-09-06T08:10:49Z")

</div>

I’m also wondering what this package does exactly, and whether it fits my needs: [GitHub - schmrlng/CPUTime.jl: Julia module for CPU timing](https://github.com/schmrlng/CPUTime.jl)

EDIT: apparently that is exactly what I needed.
