# Using @btime and printing a Vector{Vector} yields infinite loop

**URL:** https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289
**Category:** General Usage
**Tags:** package
**Created:** [February 25, 2025, 12:34pm UTC](https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289 "2025-02-25T12:34:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bernd\_Blasius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernd_blasius/32/18505_2.png) [@Bernd\_Blasius](https://discourse.julialang.org/u/Bernd_Blasius)
#### Post date: [February 25, 2025, 12:34pm UTC](https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289/1 "2025-02-25T12:34:09Z")

</div>

The following code produces an infinite loop

```julia
using BenchmarkTools
v = Vector{Vector}(undef,1)
@btime println(v)

```

Is this a bug or intended behaviour?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 25, 2025, 12:38pm UTC](https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289/2 "2025-02-25T12:38:09Z")

</div>

I can’t replicate this, I get (after a lot of output, because it’s not really sensible to call `@btime` on `println`):

```julia
(...)
Vector[#undef]
Vector[#undef]
Vector[#undef]
Vector[#undef]
  293.800 μs (340 allocations: 69.92 KiB)

```

`Chairmarks` is a bit faster here, so I guess `BenchmarkTools` uses too many samples which are expensive as you’re spamming the console:

```julia
using Chairmarks

@b println($v)

```

---

<div class="post-metadata">

### Author: ![Bernd\_Blasius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernd_blasius/32/18505_2.png) [@Bernd\_Blasius](https://discourse.julialang.org/u/Bernd_Blasius)
#### Post date: [February 25, 2025, 1:21pm UTC](https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289/3 "2025-02-25T13:21:34Z")

</div>

Ah, right. The loop is not infinite, but will stop after quite a lot of output.

Note that I did not call `@btime` on `println` directly, but on a function that (beside other things) also calls `println`

```julia
function foo(v)
   # do stuff ...
   println(v)
end
@btime foo(v)

```

So what is the recommended way to `@btime` such functions? Erase all `println` statements, or use Chairmarks instead?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 25, 2025, 2:38pm UTC](https://discourse.julialang.org/t/using-btime-and-printing-a-vector-vector-yields-infinite-loop/126289/4 "2025-02-25T14:38:35Z")

</div>

There’s not really a difference between BenchmarkTools and Chairmarks here, except that for your example Chairmarks chooses fewer samples and finishes faster (although that’s generally a goal of Chairmarks).

In my view, if you’re using `@btime` or `@b` you’re doing so because you are looking at a performance-critical part of your code. That is inherently incompatible with printing to the terminal, in general I would say but especially in Julia, see:

> [@Julia seems an order of magnitude slower than Python when printing to the terminal, because of issue with "sleep"](https://discourse.julialang.org/t/julia-seems-an-order-of-magnitude-slower-than-python-when-printing-to-the-terminal-because-of-issue-with-sleep/78151/3):
>
> Some references: [JuliaLang/julia#36639](https://github.com/JuliaLang/julia/issues/36639), [JuliaLang/julia#43176](https://github.com/JuliaLang/julia/issues/43176), [Why is printint to a terminal slow (Discourse)](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987).

So yes, I’d say if you want to benchmark the runtime of some piece of code for which you care about performance, don’t let that code print to the terminal.
