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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |