I have adapted another small benchmark to D. This benchmark is less interesting 
than the other ones because it mostly tests the optimizations done by the 
back-end. This means it's not a problem of the D language or its front-end, so 
even if DMD here shows to be not much efficient, LDC once finished may show 
significant improvements.
As usual I may have done several errors, so keep your eyes open.

D code:

/*
original code copyright 2004 Christopher W. Cowell-Shah
http://www.cowell-shah.com/research/benchmark/code
other code portions copyright
http://dada.perl.it/shootout/
and Doug Bagley
http://www.bagley.org/~doug/shootout
combined, modified and fixed by Thomas Bruckschlegel - 
http://www.tommti-systems.com
*/

import std.c.stdio: printf;
import std.c.time: clock, CLOCKS_PER_SEC, clock_t;


void longArithmetic(long longMin, long longMax) {
        clock_t startTime = clock();

    long longResult = longMin;
    long i = longMin;
        while (i < longMax)     {
                longResult -= i++;
                longResult += i++;
                longResult *= i++;
                longResult /= i++;
        }

    clock_t stopTime = clock();
    double elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / 1000.0);
        printf("Long arithmetic elapsed time: %1.0f ms with longMax %ld\n", 
elapsedTime, longMax);
        printf(" i: %ld\n", i);
        printf(" longResult: %ld\n", longResult);
}


void nested_loops(int n) {
        clock_t startTime = clock();
        int a, b, c, d, e, f;
        int x=0;

    for (a=0; a<n; a++)
                for (b=0; b<n; b++)
                        for (c=0; c<n; c++)
                                for (d=0; d<n; d++)
                                        for (e=0; e<n; e++)
                                                for (f=0; f<n; f++)
                                                        x+=a+b+c+d+e+f;

    clock_t stopTime = clock();
    double elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / 1000.0);
        printf("Nested Loop elapsed time: %1.0f ms %d\n", elapsedTime, x);
}

int main() {
    long longMin =     10_000_000_000L;
    long longMax =     11_000_000_000L;

    longArithmetic(longMin, longMax);
    nested_loops(40);
    return 0;
}

------------------------

C code, almost the same (you may need to change LL_FORMAT to make it run 
correctly):

/*
original code copyright 2004 Christopher W. Cowell-Shah
http://www.cowell-shah.com/research/benchmark/code
other code portions copyright
http://dada.perl.it/shootout/
and Doug Bagley
http://www.bagley.org/~doug/shootout
combined, modified and fixed by Thomas Bruckschlegel - 
http://www.tommti-systems.com
*/

#include "time.h"
#include "stdio.h"

// accopding to your compiler
#define LL_FORMAT "%I64d"
//#define LL_FORMAT "%ld"


void longArithmetic(long long longMin, long long longMax) {
    clock_t startTime = clock();

    long long longResult = longMin;
    long long i = longMin;
    while (i < longMax) {
        longResult -= i++;
        longResult += i++;
        longResult *= i++;
        longResult /= i++;
    }

    clock_t stopTime = clock();
    double elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / 1000.0);
    printf("Long arithmetic elapsed time: %1.0f ms with longMax "LL_FORMAT"\n", 
elapsedTime, longMax);
    printf(" i: "LL_FORMAT"\n", i);
    printf(" longResult: "LL_FORMAT"\n", longResult);
}


void nested_loops(int n) {
    clock_t startTime = clock();
    int a, b, c, d, e, f;
    int x=0;

    for (a=0; a<n; a++)
        for (b=0; b<n; b++)
            for (c=0; c<n; c++)
                for (d=0; d<n; d++)
                    for (e=0; e<n; e++)
                        for (f=0; f<n; f++)
                            x+=a+b+c+d+e+f;

    clock_t stopTime = clock();
    double elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / 1000.0);
    printf("Nested Loop elapsed time: %1.0f ms %d\n", elapsedTime, x);
}

int main() {
    long long longMin =     10000000000LL;
    long long longMax =     11000000000LL;

    longArithmetic(longMin, longMax);
    nested_loops(40);
    return 0;
}

-------------------

I have compiled it with GCC and DMD with:
gcc version 4.2.1-dw2 (mingw32-2)
-O3 -s

DMD v1.037
-O -release -inline

---------------------

Timings:

C gcc:
  Long arithmetic: 11.15 s
  Nested Loops: 0.11 s

D dmd:
  Long arithmetic: 63.7 s
  Nested Loops: 6.17 s

Bye,
bearophile

Reply via email to