BenchmarkTools macros with Books

That’s because inner_rows! returns nothing in this case

julia> t = @btime 1 + 1
  0.017 ns (0 allocations: 0 bytes)
2

julia> t
2

One solution is to use @benchmark instead:

But, of course, the real solution is to grab the output from stdout and show that. In a bit of a hacky way, his can be done with IOCapture.jl and # hide comments

```jl
s = """
    A = rand(100,100)
    B = rand(100,100)
    C = rand(100,100)

    function inner_rows!(C,A,B)
      for i in 1:100, j in 1:100
        C[i,j] = A[i,j] + B[i,j]
      end
    end
    c = IOCapture.capture() do # hide
    @btime inner_rows!(C,A,B)
    end # hide
    return c.output # hide
    """
sco(s; post=output_block)
```

which shows on the webpage as:

Another way is to wrap the evaluation step at a later point:

```jl
s = """
    A = rand(100,100)
    B = rand(100,100)
    C = rand(100,100)

    function inner_rows!(C,A,B)
      for i in 1:100, j in 1:100
        C[i,j] = A[i,j] + B[i,j]
      end
    end
    @btime inner_rows!(C,A,B)
    """
c = IOCapture.capture() do
    sc(s)
end
return code_block(s) * "\n\n" * output_block(c.output)
```
1 Like