is D so slow?

Dave Dave_member at pathlink.com
Sun Jun 15 22:09:31 PDT 2008


"Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
news:g336hl$10c8$1 at digitalmars.com...
> "baleog" <maccarka at yahoo.com> wrote in message 
> news:g32umu$11kq$1 at digitalmars.com...
>> Tomas Lindquist Olsen Wrote:
>>
>>> What switches did you use to compile? Not much info you're giving ...
>>
>> Ubuntu-6.06
>> dmd-2.0.14 - 40sec witth n=500
>> dmd -O -release -inline test.d
>> gdc-0.24 - 32sec
>> gdmd -O -release test.d
>> and gcc-4.0.3 - 1.5sec
>> gcc test.c
>>
>> so gcc without optimization runs 20 times faster than gdc
>> but i can't find how to suppress array bound checking
>
> Array bounds checking is off as long as you specify -release.
>
> I don't know if your computer is just really, REALLY slow, but out of 
> curiosity I tried running the D program on my computer.  It completes in 
> 1.2 seconds.
>
> Also, using malloc/free vs. new/delete shouldn't much matter in this 
> program, because you make all of three allocations, all before any loops. 
> The GC is never going to be called during the program.

I agree, but nonetheless the malloc version runs much faster on my systems 
(both Linux/Windows, P4 and Core2, all compiled w/ -O -inline -release). The 
relative performance difference gets larger as n increases:

n            malloc        GC
100        0.094         0.328
200        0.140         1.859
300        0.203         6.094
400        0.312        14.141
500        0.547        27.625

import std.conv;

void main(string[] args)
{
    if(args.length > 1)
        test(toInt(args[1]));
    else
        printf("usage: mm nnn\n");
}

version(malloc)
{
import std.c.stdlib;
}

void test(int n)
{
  version(malloc)
  {
  float* xs = cast(float*)malloc(n*n*float.sizeof);
  float* ys = cast(float*)malloc(n*n*float.sizeof);
  }
  else
  {
  float[] xs = new float[n*n];
  float[] ys = new float[n*n];
  }

  for(int i = n-1; i>=0; --i) {
    xs[i] = 1.0;
  }
  for(int i = n-1; i>=0; --i) {
    ys[i] = 2.0;
  }

  version(malloc)
  {
  float* zs = cast(float*)malloc(n*n*float.sizeof);
  }
  else
  {
  float[] zs = new float[n*n];
  }

  for (int i=0; i<n; ++i) {
    for (int j=0; j<n; ++j) {
      float s = 0.0;
      for (int k=0; k<n; ++k) {
        s = s + (xs[k + (i*n)] * ys[j + (k*n)]);
      }
      zs[j+ (i*n)] =  s;
    }
  }

  version(malloc)
  {
  free(zs);
  free(ys);
  free(xs);
  }
  else
  {
  delete xs;
  delete ys;
  delete zs;
  }
}



More information about the Digitalmars-d-learn mailing list