On Mon, Mar 02, 2020 at 06:27:22PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] > Yeah, this looked very fishy to me. ldc can do some nasty "helpful" > things to save you time! When I posted my results, I was using DMD. > > I used run.dlang.io with ldc, and verified I get the same (similar) > result as you. But searching through 5 billion integers can't be > instantaneous, on any modern hardware. [...]
Beware that LDC's over-zealous optimizer can sometimes elide an entire function call tree if it determines that the return value is not used anywhere. I've also observed that it can sometimes execute the entire function call tree at compile-time and emit just a single instruction that loads the final result. So, always check the assembly output so that you're sure the benchmark is actually measuring what you think it's measuring. To prevent the optimizer from eliding "useless" code, you need to do something with the return value that isn't trivial (assigning to a variable that doesn't get used afterwards is "trivial", so that's not enough). The easiest way is to print the result: the optimizer cannot elide I/O. T -- Freedom: (n.) Man's self-given right to be enslaved by his own depravity.