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

In reply to Pattern for Inline-C-based crunching by jsegal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.