in reply to Re^3: Why is this code so much slower than the same algorithm in C?
in thread Why is this code so much slower than the same algorithm in C?

Semi-un-related question: How do you pass values to/from the inline C code?

for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
  • Comment on Re^4: Why is this code so much slower than the same algorithm in C?
  • Download Code

Replies are listed 'Best First'.
Re^5: Why is this code so much slower than the same algorithm in C?
by BrowserUk (Patriarch) on Dec 09, 2008 at 14:06 UTC

    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.