For a fixed number of parameters (of a limited range of types), pretty much exactly as you would calling the C function from another piece of C code. (The required mappings to extract integers, doubles, string pointers etc. are done for you under the covers.)

#! perl -slw use strict; use Time::HiRes qw[ time ]; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_729090', CLEAN_AFTER_BUILD => 0; #include <stdlib.h> #include <string.h> #include <stdio.h> SV* thing( int stepI, int limitJ ) { int i, j; for( i = stepI; ; i += stepI ) { for( j = 1; j < limitJ; j++) { if( i % j ) break; } if( j == limitJ ) { return newSViv( i ); break; } } } END_C my $start = time; print thing( @ARGV ); print time - $start; __END__ C:\test>729090-IC 15 15 360360 0.00263190269470215 C:\test>729090-IC 20 20 232792560 1.421875 C:\test>729090-IC 23 23 698377680 30.453125

If you want to pass a list al la Perl subs, then it gets a bit more complex. Then you have to start manipulating the Perl stack yourself. Likewise if you want to return more than the C normal of one parameter.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^5: Why is this code so much slower than the same algorithm in C? by BrowserUk
in thread Why is this code so much slower than the same algorithm in C? by wanna_code_perl

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.