Welcome to Web-News
A Web-based News Reader
Subject Basic benchmark
From bearophile <bearophileHUGS@lycos.com>
Date Sat, 13 Dec 2008 10:21:53 -0500
Newsgroups digitalmars.D

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

Recent messages in this thread
 
-# Basic benchmark (Current message) bearophile 13-Dec-2008 10:21 am
.-# Re: Basic benchmark Tomas Lindquist O... 13-Dec-2008 11:16 am
.|-# Re: Basic benchmark Jarrett Billingsley 13-Dec-2008 11:37 am
.|.-# Re: Basic benchmark dsimcha 13-Dec-2008 11:49 am
.|.|-# LDC Windows exception handling Christian Kamm 13-Dec-2008 03:13 pm
.|.|.-# Re: LDC Windows exception handling Bill Baxter 13-Dec-2008 04:45 pm
.|.|.|-# Re: LDC Windows exception handling aarti_pl 13-Dec-2008 05:55 pm
.|.|.||\# Re: LDC Windows exception handling Bill Baxter 13-Dec-2008 06:08 pm
.|.|.|\# Re: LDC Windows exception handling Jérôme M. Berger 14-Dec-2008 03:29 am
.|.|.-# Re: LDC Windows exception handling dsimcha 13-Dec-2008 06:16 pm
.|.|.|-# Re: LDC Windows exception handling Aarti_pl 15-Dec-2008 08:21 am
.|.|.|.\# Re: LDC Windows exception handling Aarti_pl 16-Dec-2008 07:32 am
.|.|.\# Re: LDC Windows exception handling Mosfet 15-Dec-2008 08:16 am
.|.-# Re: Basic benchmark Jason House 13-Dec-2008 12:55 pm
.|.|-# Re: Basic benchmark Jarrett Billingsley 13-Dec-2008 01:07 pm
.|.|||# Re: Basic benchmark Fawzi Mohamed 13-Dec-2008 01:25 pm
.|.||-# Re: Basic benchmark Walter Bright 14-Dec-2008 01:22 am
.|.||.-# Re: Basic benchmark bearophile 14-Dec-2008 08:33 am
.|.||.|-# Re: Basic benchmark dennis luehring 14-Dec-2008 10:52 am
.|.||.|.-# Re: Basic benchmark bearophile 14-Dec-2008 10:56 am
.|.||.|..-# Re: Basic benchmark dennis luehring 14-Dec-2008 02:05 pm
.|.||.|...\# Re: Basic benchmark dennis luehring 14-Dec-2008 02:09 pm
.|.||.-# Re: Basic benchmark Bill Baxter 14-Dec-2008 09:07 pm
.|.||..-# Re: Basic benchmark Walter Bright 14-Dec-2008 09:37 pm
.|.||..||# Re: Basic benchmark dsimcha 14-Dec-2008 10:01 pm
.|.||..|-# Re: Basic benchmark Bill Baxter 14-Dec-2008 10:05 pm
.|.||..|.\# Re: Basic benchmark Walter Bright 15-Dec-2008 12:15 am
.|.||..-# Re: Basic benchmark naryl 14-Dec-2008 10:37 pm
.|.||..|\# Re: Basic benchmark Bill Baxter 14-Dec-2008 10:52 pm
.|.||..-# Re: Basic benchmark Don 16-Dec-2008 07:43 am
.|.||...\# Re: Basic benchmark Bill Baxter 16-Dec-2008 06:23 pm
.|.|-# Re: Basic benchmark Bill Baxter 13-Dec-2008 02:25 pm
.|.|.-# Re: Basic benchmark Don 13-Dec-2008 02:41 pm
.|.|.|\# Re: Basic benchmark Bill Baxter 13-Dec-2008 03:01 pm
.|.|.-# Re: Basic benchmark Jason House 13-Dec-2008 07:15 pm
.|.|..|# Re: Basic benchmark Bill Baxter 13-Dec-2008 07:36 pm
.|.|..-# Re: Basic benchmark dsimcha 13-Dec-2008 09:07 pm
.|.|...-# Re: Basic benchmark Lars Ivar Igesund 14-Dec-2008 05:23 am
.|.|....-# Re: Basic benchmark Bill Baxter 14-Dec-2008 07:11 am
.|.|.....\# Re: Basic benchmark Lars Ivar Igesund 14-Dec-2008 11:33 am
.|.-# Re: Basic benchmark Walter Bright 14-Dec-2008 09:04 pm
.|.|-# Re: Basic benchmark Jason House 15-Dec-2008 12:01 am
.|.|.-# Re: Basic benchmark Walter Bright 15-Dec-2008 12:13 am
.|.|..\# Re: Basic benchmark Bill Baxter 15-Dec-2008 02:23 am
.|.-# Re: Basic benchmark Andrei Alexandrescu 15-Dec-2008 09:09 pm
.|..-# Re: Basic benchmark Bill Baxter 15-Dec-2008 09:28 pm
.|...-# Re: Basic benchmark Denis Koroskin 15-Dec-2008 10:00 pm
.|....-# Re: Basic benchmark Bill Baxter 15-Dec-2008 10:23 pm
.|.....\# Re: Basic benchmark Denis Koroskin 15-Dec-2008 10:35 pm
.-# Re: Basic benchmark Walter Bright 13-Dec-2008 02:53 pm
..\# Re: Basic benchmark bearophile 13-Dec-2008 03:45 pm