Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl & C

by jsegal (Friar)
on Mar 29, 2005 at 17:58 UTC ( [id://443211]=note: print w/replies, xml ) Need Help??


in reply to Perl & C

Like others above, I would recommend Inline::C as an "easy" way to do XS. There are some tricks you can even do to let vanilla perl handle your memory management -- see for example the snippet at Pattern for Inline-C-based crunching. That snippet talks about using a packed perl string as the data source to pass data in to a C function, but the exact same technique could be used to get data out (if you know how big the result will be).

For example, if you knew you were passing in an array of 15 doubles and were getting back an array of 15 doubles (with the obvious extension if the sizes were not the same), you could do:

$inval = pack("d*",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) $results = pack("d*",(0)x15) $summary_result = c_function($inval,$results,15) @real_results = unpack("d*",$results) use Inline C => <<'END_OF_C_CODE'; double c_function(char * the_data, char * results,int nelems) { double *real_data = (double *) the_data; double *real_results = (double *) results; double summary_result; /* do your fast calculation here, storing elements in *real_resul +ts */ return summary_result; } END_OF_C_CODE


--JAS

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://443211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-28 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found