Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by eye (Chaplain)
on Dec 09, 2008 at 04:31 UTC ( [id://729077]=note: print w/replies, xml ) Need Help??


in reply to Why is this code so much slower than the same algorithm in C?

Short answer: Perl is an interpreted language and C is a compiled language.

Long answer: Perl code is executed by a virtual machine that is itself a binary executable for the native hardware of the host. C code is compiled to a binary executable for the native hardware of the host. That extra layer of software is inefficient. Perhaps the surprising thing is how often that inefficiency is irrelevant. It tends to become irrelevant when interacting with users, doing file I/O, doing almost anything with a network, and in a variety of other situations. Where it is relevant is in tasks that are purely CPU intensive, like that presented in your code.

Replies are listed 'Best First'.
Re^2: Why is this code so much slower than the same algorithm in C?
by JavaFan (Canon) on Dec 09, 2008 at 11:26 UTC
    No, on both your short and your long answer. Perl isn't interpreted, in the sense that a program isn't executed by taking a statement, parsing/compiling it, running it, then take the next statement and do it over again.

    Perl is more like Java. It's compiled, turned into an internal format (called "the optree") and then executed in its own virtual machine. The difference with Java is that in Java, compiling is a separate action from running the program - in Perl, compiling is the first stage of running.

    Now, for the long answer, the fact that C gets compiled to a native binary and then executed and Perl gets compiled to an optree counts for some difference, but far from all. Even if there was a Perl compiler that turned out native binaries (say there would be a Perl front end to the gcc compiler), then the Perl program would still be much slower than the C program. It's the price you pay for flexibility. In C, if you have an 'int' variable, that you just have an integer. C can just allocate 4 (or 8) bytes and it will know there's always an integer there. Perl doesn't. In Perl, if you access a variable and use it like an int, Perl will first fetch the meta data. It'll test whether the value stored is valid as an integer. Getting that integer requires another fetch (pointer + offset). If the value isn't valid as an integer, it will first have to calculate the integer value, from the string value for instance.

    Perl values (and hence, variables) come will all kinds of neat tricks. Strings become integers/floats and the other way around magically. Strings can expand in length without the programmer having to create a new variable. Variables can be tied, blessed or have some other magic attached. For all this niceness, you have to pay a price. And that prices doesn't only come in the form of memory usage. You pay a hefty runtime price as well.

Re^2: Why is this code so much slower than the same algorithm in C?
by Erez (Priest) on Dec 09, 2008 at 08:44 UTC

    Perl is an interpreted language ... Perl code is executed by a virtual machine that is itself a binary executable for the native hardware of the host

    No, and No.
    Quote Tom Christiansen (FMTEYEWTK about Compilation vs Interpretation in Perl):

    The perl executable ... has two distinct stages ... It compiles your perl program source into a parse tree. This compiler then performs various optimizations such as ... loading in certain library definitions ...
    Next comes the backend, which is certainly an interpreter of sorts; let's call it a PP interpreter for now, just because. While what it actually executes is a parse tree and not byte code per se, still we would not go wrong in classifying this backend as a byte-code interpreter (like java or python). This is useful in particular when it comes to distinguishing these languages from "pure" interpreters.

    "A core tenant of the greater Perl philosophy is to trust that the developer knows enough to solve the problem" - Jay Shirley, A case for Catalyst.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found