release mode optimization

Jonathan M Davis jmdavisProg at gmx.com
Wed Jan 9 16:40:09 PST 2013


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


More information about the Digitalmars-d-learn mailing list