# Time Calculation for Recurring Function Calls

**URL:** https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578
**Category:** Performance
**Tags:** question
**Created:** [July 3, 2024, 8:57am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578 "2024-07-03T08:57:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![usaer](https://avatars.discourse-cdn.com/v4/letter/u/b9bd4f/32.png) [@usaer](https://discourse.julialang.org/u/usaer)
#### Post date: [July 3, 2024, 8:57am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/1 "2024-07-03T08:57:34Z")

</div>

Hi all, I have a function xyz which I call at different positions inside my main function. Is there any way that I could get the cummulative sum of computational time for all the function xyz calls?

---

<div class="post-metadata">

### Author: ![maxfreu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxfreu/32/17468_2.png) [@maxfreu](https://discourse.julialang.org/u/maxfreu)
#### Post date: [July 3, 2024, 10:09am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/2 "2024-07-03T10:09:33Z")

</div>

There’s the `@timed` macro that can help. It gives you the return value of the evaluated expression as well as the time it took for evaluation.

```julia
total_time = 0.
value, t = @timed rand(3)
total_time += t

```

But what you are probably actually looking for is a profiler. If you are using VSCode, then you can use the `@profview` macro, that is documented [here](https://www.julia-vscode.org/docs/dev/userguide/profiler/). That will give you way better insight into what’s taking how much time in your code.

---

<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: [July 3, 2024, 10:11am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/3 "2024-07-03T10:11:01Z")

</div>

I think the output of `@profview` might be hard to read for recursive calls. TimerOutputs.jl is probably closer to what you need

---

<div class="post-metadata">

### Author: ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)
#### Post date: [July 3, 2024, 10:18am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/4 "2024-07-03T10:18:08Z")

</div>

The easiest way I’ve found is with TimerOutputs.jl. If you have a function:

```julia
function xyz(a, b)
  a1 = a^2
  b1 = b^3
  output = a1 + b1
  return output
end

```

You can modify it like this:

```julia
using TimerOutputs

function xyz(a, b)
  @timeit "xyz" begin
      a1 = a^2
      b1 = b^3
      output = a1 + b1
  end
  return output
end

xyz(1, 2)
print_timer()

```

You can also add `@timeit` statements to specific lines in the function if you want to drill down.

---

<div class="post-metadata">

### Author: ![usaer](https://avatars.discourse-cdn.com/v4/letter/u/b9bd4f/32.png) [@usaer](https://discourse.julialang.org/u/usaer)
#### Post date: [July 3, 2024, 10:53am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/5 "2024-07-03T10:53:05Z")

</div>

Thanks. This was exactly what I was looking for. Is there a way to clear the time output so that if i run the function again the time starts from zero

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [July 3, 2024, 10:53am UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/6 "2024-07-03T10:53:12Z")

</div>

The package `Profile` has a `print` function with several options for combining timings in various ways.

---

<div class="post-metadata">

### Author: ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)
#### Post date: [July 3, 2024, 1:04pm UTC](https://discourse.julialang.org/t/time-calculation-for-recurring-function-calls/116578/7 "2024-07-03T13:04:26Z")

</div>

`reset_timer!()`

You can find more detailed documentation on the package page: [JuliaHub](https://juliahub.com/ui/Packages/General/TimerOutputs)
