is D so slow?

Fawzi Mohamed fmohamed at mac.com
Mon Jun 16 10:28:09 PDT 2008


On 2008-06-16 18:53:48 +0200, baleog <maccarka at yahoo.com> said:

> Fawzi Mohamed Wrote:
> 
>> check your algorithm (you initialize, backwards for some strange
>> reason) just part of the arrays...
>> putting
>> xs[] = 1.0;
>> ys[] = 2.0;
>> instead of your strange loops, solves everything...
>> 
> but if i need evident init loop(replace constant to random 
> initialization)?? did you mean that in this case i must use `mallloc` 
> function

To quote myself:

> 2) NaNs
> operations involving NaNs depending on the IEEE compliance requested on 
> the processor can be 1000 times slower!!!!!!!!
> D (very thoughtfully, as it makes spotting errors easier) initializes 
> the floating point numbers with NaNs (unlike C).

your loop

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

initializes only xs[0..n]
but you have also xs[n..n*n] that have their default initial value (the 
same is valid for ys).

In D by default this value is NaN (which is good, as it helps you to 
spot errors in code that you really use, not only benchmark).
When you use these values your program goes very slow if full IEEE 
compliance is requested from your processor (at least on my pc).

If you use malloc, the default initialization does not take place, the 
memory is normally either initialized to 0, or left uninitialized (with 
values that likely are not NaN).
So your program is fast with malloc, but in fact all this is due to a 
bug in the program that you are benchmarking, and using malloc is not 
the correct solution, the solution is to initialize all the values that 
you use.

Fawzi



More information about the Digitalmars-d-learn mailing list