Small part of a program : d and c versions performances diff.

NCrashed via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 9 04:54:54 PDT 2014


On Wednesday, 9 July 2014 at 10:57:33 UTC, Larry wrote:
> Hello,
>
> I extracted a part of my code written in c.
> it is deliberately useless here but I would understand the 
> different technics to optimize such kind of code with gdc 
> compiler.
>
> it currently runs under a microsecond.
>
> Constraint : the way the code is expressed cannot be changed 
> much we need that double loop because there are other 
> operations involved in the first loop scope.
>
> main.c :
> [code]
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include "jol.h"
> #include <time.h>
> #include <sys/time.h>
> int main(void)
> {
>
>     struct timeval s,e;
>     gettimeofday(&s,NULL);
>
>     int pol = 5;
>     tes(&pol);
>
>
>     int arr[] = {9,16,458,2,68,5452,98,32,4,565,78,985,3215};
>     int len = 13-1;
>     int g = 0;
>
>     for (int x = 36; x >= 0 ; --x ){
>         // some code here erased for the test
>         for(int y = len ; y >= 0; --y){
>             //some other code here
>             ++g;
>             arr[y] +=1;
>
>         }
>
>     }
>     gettimeofday(&e,NULL);
>
>     printf("so ? %d %lu %d %d %d",g,e.tv_usec - s.tv_usec, 
> arr[4],arr[9],pol);
>     return 0;
> }
> [/code]
>
> jol.c
> [code]
> void tes(int * restrict a){
>
>     *a = 9;
>
> }
> [/code]
>
> and jol.h
>
> #ifndef JOL_H
> #define JOL_H
> void tes(int * restrict a);
> #endif // JOL_H
>
>
> Now, the D counterpart:
>
> module main;
>
> import std.stdio;
> import std.datetime;
> import jol;
> int main(string[] args)
> {
>
>
>     auto currentTime = Clock.currTime();
>
>     int pol = 5;
>     tes(pol);
>     pol = 8;
>
>     int arr[] = [9,16,458,2,68,5452,98,32,4,565,78,985,3215];
>     int len = 13-1;
>     int g = 0;
>
>     for (int x = 31; x >= 0 ; --x ){
>
>         for(int y = len ; y >= 0; --y){
>
>             ++g;
>             arr[y] +=1;
>
>         }
>
>     }
>     auto currentTime2 = Clock.currTime();
>     writefln("Hello World %d %s %d %d\n",g, (currentTime2 - 
> currentTime),arr[4],arr[9]);
>
>     return 0;
> }
>
> and
>
> module jol;
> final void tes(ref int a){
>
>     a = 9;
>
> }
>
>
> Ok, the compilation options :
> gdc hello.d jol.d -O3 -frelease -ftree-loop-optimize
>
> gcc -march=native -std=c11 -O2 main.c jol.c
>
> Now the performance :
> D : 12 µs
> C : < 1µs
>
> Where does the diff comes from ? Is there a way to optimize the 
> d version ?
>
> Again, I am absolutely new to D and those are my very first 
> line of code with it.
>
> Thanks

Clock isn't an accurate benchmark instrument. Try 
std.datetime.benchmark:
```
module main;

import std.stdio;
import std.datetime;

void tes(ref int a)
{
     a = 9;
}

int[] arr = [9,16,458,2,68,5452,98,32,4,565,78,985,3215];

void foo()
{
     int pol = 5;
     tes(pol);
     pol = 8;
     int g = 0;

     foreach_reverse(x; 0..31)
     {
         foreach_reverse(ref a; arr)
         {
             ++g;
             a += 1;
         }
     }
}

void main()
{
     auto res = benchmark!foo(1000); // take mean of 1000 launches
     writeln(res[0].msecs, " ", arr[4], " ", arr[9]);
}
```

Dmd time: 1 us
Gcc time: <= 1 us


More information about the Digitalmars-d-learn mailing list