if(false) vs if(isNaN) benchmark

monarch_dodra monarchdodra at gmail.com
Thu Jul 4 06:05:22 PDT 2013


On Thursday, 4 July 2013 at 12:53:48 UTC, Joseph Rushton Wakeling 
wrote:
> Hi all,
>
> A trivial little benchmark for comparing the performance of a 
> function using
> if(!booleanValue) as a conditional, versus one using 
> if(isNan(x)).
>
> Run with dmd -O -release -inline -noboundscheck on my system, I 
> find the two are
> virtually equivalent, with it taking about 30ms for 1 million 
> if(!boolean)
> versus about 31 for a million if(isNaN(floatingpoint)).
>
> With ldmd2 and the same optimizations, I get about 25 compared 
> to 27-28 ms for
> the two cases, again with a million realizations of each.
>
> Finally, with gdmd I get 30ms for a million if(!boolean) versus 
> about 150ms for
> a million if(isNaN(floatingpoint)).
>
> Code attached, feel free to critique.  Generally I think these 
> results show that
> the difference between checking a boolean and checking isNan 
> are negligible, but
> I'm surprised at the relatively bad performance with gdmd and I 
> wonder if
> there's some particular reason for this.
>
> The relevance is to a fix I'm preparing for this bug:
> http://d.puremagic.com/issues/show_bug.cgi?id=10322
>
> See also: 
> http://forum.dlang.org/post/lcrwybqszclitzravrqo@forum.dlang.org
>
> Thanks in advance for any feedback :-)
>
> Best wishes,
>
>     -- Joe

Me thinks the test is biased with answering true to isNan. You 
should first initialize your arrays with random [true/false] | 
[nan/nonan] value. In particular, it is more important for isNan 
to answer "No" as fast as possible, rather than actually find 
nan's. (common, use case is checking that a number is *not*) nan. 
I'd say a mix of 90% rands + 10% Nan's would be representative?

Also, there might be optimizations in bool iteration over double 
iteration given their size, as well as the fact that the "first" 
test is typically faster.

I think you should create an array of S{bool; double;} So that 
the iteration is not biased either.

I'd add a dummy "warmup" loop of 10% of the iterations too, just 
'cause.

Gotta go.


More information about the Digitalmars-d-learn mailing list