release mode optimization

Charles Hixson charleshixsn at earthlink.net
Wed Jan 9 21:48:03 PST 2013


On 01/09/2013 04:40 PM, Jonathan M Davis wrote:
> On Wednesday, January 09, 2013 16:14:47 Charles Hixson wrote:
>> Would the following code:
>> for (int i = 1; i<  di.count; i++)
>> { assert (node.di.entry[i - 1].key<  node.di.entry[i].key); }
>>
>> be optimized away if compiled under -release?
>
> If you compile with -release, assertions will be compiled out, so you'd get
>
> for(int i = 1; i<  d.count; i++)
> {}
>
> However, I don't believe that -release enables any optimizations - that's what
> -O is for - so the loop would likely be left in with -release. If you compile
> with -O, it _might_ be optimized out. I don't know. It would be great if it
> did, but you'd have to test it to know for sure. But that's not hard to do at
> all (in fact, it wouldn't have taken much longer to test it then to post your
> question). In fact, if I test it now...
>
> This code
>
> import std.conv;
> import std.datetime;
> import std.stdio;
>
> void main()
> {
>   auto sw = StopWatch(AutoStart.yes);
>
>   foreach(i; 0 .. 50_000_000)
>   {}
>
>   writeln(to!Duration(sw.peek));
> }
>
> takes just over 37ms on my machine. If I remove the foreach, it takes about 3
> hnsecs. So clearly, dmd does _not_ optimize out the loop. I have no idea what
> gdc and ldc do though.
>
> - Jonathan M Davis
Thank you for a reply that was both useful and informative.

P.S.:  Not knowing where to look, it would have taken me a lot more time 
that your estimate.  I'm not sure that a few hours would have sufficed, 
as I seem to be running into various compiler problems that I don't 
understand well enough to characterize.


More information about the Digitalmars-d-learn mailing list