Element-wise addition of arrays
Eduardo Cavazos
wayo.cavazos at gmail.com
Thu Aug 19 05:40:27 PDT 2010
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.
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
More information about the Digitalmars-d
mailing list