#! perl -slw use strict; use Devel::Peek; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'QsortPD', CLEAN_AFTER_BUILD => 0; #include #include #include int compare( const void *arg1, const void *arg2 ) { double diff = *(double*)arg1 - *(double*)arg2; return diff < 0 ? -1 : diff > 0 ? 1 : 0; } int qsortPackedDoubles( SV *data, SV *n ) { qsort( (void *)SvPVX( data ), SvUV( n ), sizeof( double ), compare ); } END_C our $N ||= 1e6; ## Warning N less that 1000 will give funny results my $packedDoubles = ''; ## Preallocate the scalar and random fill with packed doubles open RAM, '>', \$packedDoubles; seek RAM, $N*8, 0; print RAM chr(0); seek RAM, 0, 0; printf RAM "%s", pack 'd1000', map{ rand 32767 } 1 .. 1000 for 1 .. $N / 1000; close RAM; print "Starting sort of $N packed doubles: ", time; qsortPackedDoubles( $packedDoubles, $N ); print "Finished sort of $N packed doubles: ", time; <>; print for unpack 'd*', $packedDoubles;