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

Larry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 9 03:57:29 PDT 2014


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


More information about the Digitalmars-d-learn mailing list