## digitalmars.D - Element-wise addition of arrays

• Eduardo Cavazos (30/30) Aug 19 2010 Hello,
• Don (6/43) Aug 19 2010 That's correct. There is a substantial overhead in the current
```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  a ;
double  b = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;
double  c = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;

void add ( double  a , double  b , double  c )
{
a = b + c ;
a = b + c ;
}

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.

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.

Ed
```
Aug 19 2010
```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  a ;
double  b = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;
double  c = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;

void add ( double  a , double  b , double  c )
{
a = b + c ;
a = b + c ;
}

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.
```
Aug 19 2010