how to benchmark pure functions?

ab not_a_real_address at nowhere.ab
Fri Oct 28 09:48:14 UTC 2022


On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote:
> Hi,
>
> when trying to compare different implementations of the 
> optimized builds of a pure function using benchmark from 
> std.datetime.stopwatch, I get times equal to zero, I suppose 
> because the functions are not executed as they do not have side 
> effects.
>
> The same happens with the example from the documentation:
> https://dlang.org/library/std/datetime/stopwatch/benchmark.html
>
> How can I prevent the compiler from removing the code I want to 
> measure? Is there some utility in the standard library or 
> pragma that I should use?
>
> Thanks
>
> AB

Thanks to H.S. Teoh and Dennis for the suggestions, they both 
work. I like the empty asm block a bit more because it is less 
invasive, but it only works with ldc.

@Imperatorn see Dennis code for an example. 
std.datetime.benchmark works, but at high optimization level 
(-O2, -O3) the loop can be removed and the time brought down to 
0hnsec. E.g. try "ldc2 -O3 -run dennis.d".

AB


More information about the Digitalmars-d-learn mailing list