# Measuring time of only a part of a for-loop

**URL:** https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445
**Category:** General Usage
**Created:** [March 5, 2022, 3:55pm UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445 "2022-03-05T15:55:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Handam](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Handam](https://discourse.julialang.org/u/Handam)
#### Post date: [March 5, 2022, 3:55pm UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445/1 "2022-03-05T15:55:47Z")

</div>

Hi,

is there a way to the following:

```julia
for i in 1:100
	if i == 2
		@time begin
	elseif i == 88
		end
	end
end

```

Obviously this code does not work, but I think it makes it clear what I want to achieve: measuring the time that is needed in the loop for 2 \leq i \leq 88. Is this somehow possible? Thanks in advance!

Best regards,  
Handam

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 5, 2022, 4:07pm UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445/2 "2022-03-05T16:07:08Z")

</div>

I think [GitHub - KristofferC/TimerOutputs.jl: Formatted output of timed sections in Julia](https://github.com/KristofferC/TimerOutputs.jl) should do what you’re looking for.

---

<div class="post-metadata">

### Author: ![Handam](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Handam](https://discourse.julialang.org/u/Handam)
#### Post date: [March 5, 2022, 5:18pm UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445/3 "2022-03-05T17:18:17Z")

</div>

Thanks for the quick answer!

I am still not sure what the best way is to solve my problem with the TimerOutputs package. I guess one solution would be

```julia
function instructions()
	#Some instructions
end

const to = TimerOutput()
for i in 1:100
	if i>=2 && i<= 88
		@timeit to "part_of_loop" instructions()
	else
		instructions()
	end
end

```

but that feels a little bit odd.  
Other things I tried, which didn’t work though, were for example:

```julia
const to = TimerOutput()
disable_timer!(to)
@timeit to "part_of_loop" begin
	for i in 1:100
		if i == 2
			enable_timer!(to)
		elseif i == 88
			disable_timer!(to)
		end
		#Some instructions
	end
end

```

Any better solution than my first example?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [March 6, 2022, 2:28am UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445/4 "2022-03-06T02:28:41Z")

</div>

One ugly way, but very effective, way is to triplicate your loop: copy the loop body three times and change the respective intervals to 1:1, 2:88, and 89:100; then wrap only the second loop with timing.

---

<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: [March 6, 2022, 7:07am UTC](https://discourse.julialang.org/t/measuring-time-of-only-a-part-of-a-for-loop/77445/5 "2022-03-06T07:07:13Z")

</div>

You could get your second example to work with:

```julia
const to = TimerOutput()
disable_timer!(to)
	for i in 1:100
		if i == 2
			enable_timer!(to)
		elseif i == 88
			disable_timer!(to)
		end
		@timeit to "part_of_loop" begin
                #instructions
        end
	end
end

```
