in reply to Re^2: 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?

You don't have throw the baby out with the bath water in order to benefit from good performance:

#! perl -slw use strict; 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 i, j; for (i = 20; ; i += 20) { for (j = 1; j < 20; j++) { if (i % j) break; } if (j == 20) { return newSViv( i ); break; } } } END_C print time; print thing(); print time; __END__ C:\test>729090-IC.pl 1228813945 232792560 1228813946

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."
  • Comment on Re^3: Why is this code so much slower than the same algorithm in C?
  • Download Code

Replies are listed 'Best First'.
Re^4: Why is this code so much slower than the same algorithm in C?
by pobocks (Chaplain) on Dec 09, 2008 at 13:38 UTC

    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";

      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.