# BenchmarkTools macros with Books

**URL:** <https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540>\
**Category:** Tooling\
**Tags:** books\
**Created:** [January 31, 2022, 8:51pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540 "2022-01-31T20:51:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvaverka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jvaverka/32/19892_2.png) [@jvaverka](https://discourse.julialang.org/u/jvaverka)\
**Post date:** [January 31, 2022, 8:51pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/1 "2022-01-31T20:51:38Z")

</div>

Given this example `weave`d output from a Julia Markdown Document:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f4c5ff59f0fd451f1b01995fd2713c71293389a.png)

Generated with this code block:

```julia
A = rand(100,100)
B = rand(100,100)
C = rand(100,100)
using BenchmarkTools
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)

```

How might I achieve the same effect using [Books.jl](https://github.com/JuliaBooks/Books.jl)?

So far in my module I have defined a function, `inner_rows!`:

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

```

Which is called on the page with 3 `jl` code blocks:

```julia
sc("using BenchmarkTools")

```

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

```

```julia
scob("@btime BookTest.inner_rows!(C,A,B) |> string")

```

Running `gen()` is successful, but upon `serve()` the macro output is not shown. Instead only the return value, `nothing`, is shown.

![image](https://global.discourse-cdn.com/julialang/original/3X/0/c/0cdcc872d5e98b7f4b7f718ae41aa32e7696e726.png)

Is there a way to do this in Books.jl?

---

<div class="post-metadata">

**Author:** ![jvaverka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jvaverka/32/19892_2.png) [@jvaverka](https://discourse.julialang.org/u/jvaverka)\
**Post date:** [January 31, 2022, 8:54pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/2 "2022-01-31T20:54:54Z")

</div>

Note also that I added `|> string` after trying this code block:

```julia
scob("@btime BookTest.inner_rows!(C,A,B)")

```

Produced the following result. Probably I am going wrong somewhere. This is my first trial run with Books.jl

![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a14f6ae1df8416880b63d2b9010379ab5a21fde.png)

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [January 31, 2022, 9:54pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/3 "2022-01-31T21:54:00Z")

</div>

That’s because `inner_rows!` returns `nothing` in this case

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

julia> t
2

```

One solution is to use `@benchmark` instead:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af85633c7a653eb395e876fde62dbf5c7d798a58.png)

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/a/da02bd4c024460a5c0b04b5fc3e0263bf34ae7d8.png)

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

````

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [January 31, 2022, 9:55pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/4 "2022-01-31T21:55:59Z")

</div>

The problem with the backticks in your post is probably because you didn’t put a newline before and after each code block. That’s a parser problem from Pandoc. I could fix that by always adding a newline before and after each code block, but it seems like a bit of a waste so I didn’t do that (yet).

---

<div class="post-metadata">

**Author:** ![jvaverka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jvaverka/32/19892_2.png) [@jvaverka](https://discourse.julialang.org/u/jvaverka)\
**Post date:** [January 31, 2022, 11:53pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/5 "2022-01-31T23:53:17Z")

</div>

> [@rikh](#):
>
> That’s a parser problem from Pandoc

Yeah, not a big problem.

---

<div class="post-metadata">

**Author:** ![jvaverka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jvaverka/32/19892_2.png) [@jvaverka](https://discourse.julialang.org/u/jvaverka)\
**Post date:** [January 31, 2022, 11:57pm UTC](https://discourse.julialang.org/t/benchmarktools-macros-with-books/75540/6 "2022-01-31T23:57:26Z")

</div>

This solution is exactly what I was looking for! Thank you for pointing me in the right direction, and great work on the package!
