J
Jonas Nilsson
Hello.
I have an optimization problem.
I have two arrays of floats (millions of items in each). I need the absolut
sum of each pairwise difference.
I would prefer to have the arrays as packed arrays (double or single
precision will both work) since I would otherwise need to much memory.
I need to do this quite quickly. The code that i now use takes about 5 days
to run on a 2GHz pentium. Is there any trick, with bitwise operators or some
other tweak what would significantly speed op this code? I'm sad so say that
I don't have the knowledge about using C.
/jN
Some example code just for you to see what I wan't to acheve.
use strict;
use Benchmark;
my $count=1e5; #Just for testing
my @array1=map {rand()-0.5} (1..$count);
my @array2=map {rand()-0.5} (1..$count);
my $array1_f=pack "f*",@array1;
my $array2_f=pack "f*",@array2;
my $array1_d=pack "d*",@array1;
my $array2_d=pack "d*",@array2;
timethese(20,{
'array'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs($array1[$_]-$array2[$_]);
}
},
'scalar of f'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs(unpack('f',substr($array1_f,$_*4,4))-unpack('f',substr($array2_
f,$_*4,4)));
}
},
'scalar of d'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs(unpack('d',substr($array1_d,$_*8,8))-unpack('d',substr($array2_
d,$_*8,8)));
}
},
});
I have an optimization problem.
I have two arrays of floats (millions of items in each). I need the absolut
sum of each pairwise difference.
I would prefer to have the arrays as packed arrays (double or single
precision will both work) since I would otherwise need to much memory.
I need to do this quite quickly. The code that i now use takes about 5 days
to run on a 2GHz pentium. Is there any trick, with bitwise operators or some
other tweak what would significantly speed op this code? I'm sad so say that
I don't have the knowledge about using C.
/jN
Some example code just for you to see what I wan't to acheve.
use strict;
use Benchmark;
my $count=1e5; #Just for testing
my @array1=map {rand()-0.5} (1..$count);
my @array2=map {rand()-0.5} (1..$count);
my $array1_f=pack "f*",@array1;
my $array2_f=pack "f*",@array2;
my $array1_d=pack "d*",@array1;
my $array2_d=pack "d*",@array2;
timethese(20,{
'array'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs($array1[$_]-$array2[$_]);
}
},
'scalar of f'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs(unpack('f',substr($array1_f,$_*4,4))-unpack('f',substr($array2_
f,$_*4,4)));
}
},
'scalar of d'=>sub {
my $abssum=0;
for (0..$count-1) {
$abssum+=abs(unpack('d',substr($array1_d,$_*8,8))-unpack('d',substr($array2_
d,$_*8,8)));
}
},
});