Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Why is this code so much slower than the same algorithm in C?

by wanna_code_perl (Pilgrim)
on Dec 09, 2008 at 07:59 UTC ( #729101=note: print w/ replies, xml ) Need Help??


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

and you are not allowed to put it in a batch or shell script, because that is part of another language

What? Well, OK, since it's pretty tough to automate compilation and execution without some sort of "script", the following example takes my corrected typing speed into account, too!

pts/0 $> date Mon Dec 8 23:40:17 PST 2008 pts/0 $> gcc p5.c pts/0 $> ./s.^H^Ha.out Number: 232792560 pts/0 $> date Mon Dec 8 23:40:21 PST 2008

Still less than four seconds, and most of that was thanks to my clumsy typing. Fair enough, no?

Anyway, compile time is irrelevant for a bunch of reasons, but mainly: compile time will be negligible for any CPU-bound problem of significant size. Such problems are more or less the point of this node.

Finally, to really look at the compilation step fairly, how many real-world programs (including real-world Perl programs) really need to be recompiled every time they are run? For many applications, an "interpreted" compilation step is just another nail in the comparative performance coffin.


Comment on Re^2: Why is this code so much slower than the same algorithm in C?
Download Code
Re^3: Why is this code so much slower than the same algorithm in C?
by CountZero (Bishop) on Dec 09, 2008 at 08:28 UTC
    This is indeed a fair comparison.

    Of course, if you want high execution speed, Perl is not a wise choice. you should choose Assembler or pure machine code then. In almost all other cases though, Perl is "fast enough" and much more convenient to use.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^3: Why is this code so much slower than the same algorithm in C?
by BrowserUk (Pope) on Dec 09, 2008 at 09:14 UTC

    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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-09-02 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls