Perl is great for most things, but if you are doing number crunching with lots of data, it is hard to beat C for speed. Inline::C provides an easy way to connect C and perl while
pack can provide the data link. Put them together, and you get the following pattern. I suppose you could call it a "poor-man's PDL."
(Note -- this snippet is not runnable "out of the box," but demonstrates the pattern of how to set this up for those who may not be fluent in both C and perl).
# ....
# use perl to get your data into an array however suits
# your needs (from a file, DBI, etc.)
@array = (1,2,3,4,5,6,7,23); # for example
# sample case where the C function is signed-int-based
$packed_as_ints = pack("i*",@array);
# note that with C and arrays, you need to pass the number
# of elements along too
$result = fast_c_function_int($packed_as_ints,scalar(@array));
# here is a sample case using doubles
$packed_as_doubles = pack("d*",@array);
$result = fast_c_function_double($packed_as_doubles,scalar(@array));
# etc. -- this technique can work for any primitive
# datatype that can be represented natively (byte, short, etc)
# see perldoc -f pack for the codes to pack to any of the
# native data types (note where you need the "!" to force
# native packing, though!)
use Inline C => <<'END_OF_C_CODE';
double fast_c_function_int(char * the_data, int nelems) {
int *real_data = (int *) the_data;
double result;
/* do your fast calculation here */
return result;
}
double fast_c_function_double(char * the_data, int nelems) {
double *real_data = (double *) the_data;
double result;
/* do your fast calculation here */
return result;
}
END_OF_C_CODE