bheisler / iai

Experimental one-shot benchmarking/profiling harness for Rust
Apache License 2.0
586 stars 24 forks source link

Allow setup code to run outside of benchmark #20

Open ndrewxie opened 3 years ago

ndrewxie commented 3 years ago

Sorry if this is something that already exists, but I'm unable to find it in the docs:

Let's say I have something I want to benchmark a few methods of a struct, but instantiating and setting up the struct is expensive. There should be a feature where I can instantiate and set up the struct outside of the benchmark, and then pass it into the benchmark to benchmark the relevant methods.

Mart-Bogdan commented 1 year ago

Hello, @ndrewxie!

You can use cargo-expand to see how iai::main! is expaneded, and copy generated code into your source, and than do any set-up and initialization you like.

P.S. I think it would be beneficial to add second macro, that generates not main, but regular function, that can be called from real main by human.