# Rb Tree, Benchmarking rate per CPU second

**URL:** <https://discourse.julialang.org/t/rb-tree-benchmarking-rate-per-cpu-second/71072>\
**Category:** New to Julia\
**Created:** [November 6, 2021, 11:17pm UTC](https://discourse.julialang.org/t/rb-tree-benchmarking-rate-per-cpu-second/71072 "2021-11-06T23:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnemonic](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mnemonic](https://discourse.julialang.org/u/mnemonic)\
**Post date:** [November 6, 2021, 11:17pm UTC](https://discourse.julialang.org/t/rb-tree-benchmarking-rate-per-cpu-second/71072/1 "2021-11-06T23:17:41Z")

</div>

Is there a way to benchmark a block of Julia code in rate per CPU second?  
Example in Perl, see “timethese” [How to benchmark Perl code for speed](https://www.perl.com/article/40/2013/9/29/How-to-benchmark-Perl-code-for-speed/)

It outputs something like  
10 wallclock secs (10.32 usr + 0.00 sys = 10.32 CPU) @ 150112.98/s (n=1549166)

Where i am interested in the “150112.98/s” which is the rate per CPU second.

Another question, is there any Red and Black Tree library other than the default one from DataStructures? From what i could check It misses very basic functions.

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [November 8, 2021, 12:58am UTC](https://discourse.julialang.org/t/rb-tree-benchmarking-rate-per-cpu-second/71072/2 "2021-11-08T00:58:26Z")

</div>

I don’t know the answer to your first question. As for your second question, please open an issue on the DataStructures issues page ([https://github.com/JuliaCollections/DataStructures.jl/issues](https://github.com/JuliaCollections/DataStructures.jl/issues)) for the missing features of red-black trees that you need. Also, note that the sorted containers in DataStructures.jl have more features than the current implementation of red-black trees. The sorted containers are based on 2-3 trees, another kind of balanced-tree data structure.

---

<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:** [November 8, 2021, 3:31am UTC](https://discourse.julialang.org/t/rb-tree-benchmarking-rate-per-cpu-second/71072/3 "2021-11-08T03:31:38Z")

</div>

If I’m understanding that link correctly, I think you could get the result you’re interested in by just taking the reciprocal of the mean time from `BenchmarkTools` `@benchmark`:

```julia
julia> f(x) = sqrt(x) + 1
f (generic function with 1 method)

julia> using BenchmarkTools

julia> benchmark_result = @benchmark f(x) setup=(x = rand())
BechmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max): 1.448 ns … 19.763 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 1.461 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 1.473 ns ± 0.319 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▁█▅▄▃▁                                                      
  ██████▅▃▂▃▂▂▂▁▂▂▁▁▂▂▁▁▂▂▁▁▁▂▂▂▂▂▁▂▁▁▁▂▂▂▂▂▂▂▁▁▂▁▂▂▂▂▂▁▁▁▁▂ ▃
  1.45 ns Histogram: frequency by time 1.69 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

```julia
# time(mean(benchmark_result)) gives the mean time per execution in ns
julia> 1 / (time(mean(benchmark_result)) * 1e-9)
6.79023168134692e8

```

(Although the mean may not actually be as informative, since it can be heavily influenced by, for example, your CPU switching off to work on something else in the middle of a run. The minimum or median time may be more useful in comparing functions.)
