Eduardo Cavazos wrote:
Hello,

Here's a short program which compares via 'benchmark' two ways to perform element-wise addition of two arrays.

----------------------------------------------------------------------
import std.stdio ;
import std.date ;
import std.random ;

void main ()
{
  double [2] a ;
  double [2] b = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;
  double [2] c = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;

  void add ( double [2] a , double [2] b , double [2] c )
  {
    a[0] = b[0] + c[0] ;
    a[1] = b[1] + c[1] ;
  }

  void f0 () { add ( a , b , c ) ; }

  void f1 () { a[] = b[] + c[] ; }

  writeln ( benchmark ! ( f0 , f1 ) ( 10_000_000 ) ) ;
}
----------------------------------------------------------------------

On my system, it seems that 'f1' is slower than 'f0'. Let me know if somethings not right with the test program or if there's a better way to do the benchmark.

That's correct. There is a substantial overhead in the current implementation of array operations. My guess is that the array length needs to be about 10 or so before array ops start to win.

Anywho my question is, in principle, isn't enough information available to the compiler such that it can make 'f1' be as fast as 'f0'? I'm just wondering if I'll need to make functions like the above 'add' which are specific for 2 and 3 element arrays.

Yes, it's a simple implementation issue, which is on the TODO list. It shouldn't be terribly difficult to do.

Reply via email to