Functional vs simple code

ixid nuaccount at gmail.com
Tue Oct 2 15:58:43 PDT 2012


On Tuesday, 2 October 2012 at 22:13:10 UTC, Timon Gehr wrote:
> On 10/03/2012 12:11 AM, Timon Gehr wrote:
>> ...
>>
>> $ cat ixidbench.d
>> module main;
>> import std.stdio, std.algorithm, std.range, std.datetime;
>>
>> enum MAX = 10_000_000_000UL;
>>
>> void main() {
>>     StopWatch sw;
>>     sw.start;
>>
>>     auto sum1 = MAX.iota.map!(x => x * x).reduce!"a + b";
>>
>>     sw.stop;
>>     sw.peek.msecs.writeln("msecs");
>>     sum1.writeln;
>>     sw.reset;
>>     sw.start;
>>
>>     ulong sum2 = 0;
>>     foreach(i;0..MAX)
>>         sum2 += i * i;
>>
>>     sw.stop;
>>     sw.peek.msecs.writeln("msecs");
>>
>>     sum2.writeln;
>> }
>> $ ldmd2 -O -release -inline ixidbench.d -ofixidbench
>> $ ./ixidbench
>> 6528msecs
>> 7032546979563742720
>> 7518msecs
>> 7032546979563742720
>
> $ gdmd -O -release -inline ixidbench.d -ofixidbench
> $ ./ixidbench
> 11250msecs
> 7032546979563742720
> 11233msecs
> 7032546979563742720

Yes, I think it was just the compiler and compiler options rather 
than anything inherent in the method, DMD produced much slower 
code than GDC for the functional version. It's interesting that 
it's significantly faster in the functional style with LDC.


More information about the Digitalmars-d-learn mailing list