in reply to compiling perl scripts aka why is perl not as fast as C

why is it that I can't compile it and get the same level of speed that a native C program would get?
Because compiling still doesn't give you the speed C gives. Take for instance strings. In C, strings are just pointers to arrays with numbers. You cannot, in a single operation, add to a string. And it's even a lot harder to have all other references to such a string see the change.

In Perl, this is a lot easier. I can easily add to a string. If there's a reference to the string I'm modifying, the reference will see the change. All this makes that Perl is a lot slower than C - it's all the goodies that Perl gives to the programmer that makes it slower, that has a much bigger impact that the difference between compiled and not-compiled.

If you need speed, by all means, use C. If you are willing to sacrifice speed for programmer niceties, you have the option to use Perl.

  • Comment on Re: compiling perl scripts aka why is perl not as fast as C

Replies are listed 'Best First'.
Re^2: compiling perl scripts aka why is perl not as fast as C
by nikosv (Deacon) on Mar 22, 2010 at 09:51 UTC

    I totally agree.

    I don't think that there is a reason to write in C nowadays except in embedded systems where real time speed is crucial.C was being used because its structures map closely to the hardware.

    Perl uses references which disallow pointer arithmetic or pointer de-referencing which of course are slower in operation than pointers, but at the same time you don't get involved with allocating and de-allocating memory or playing directly with the hardware generating sigfaults easily.

    Other issues that arise from C's non-restriction and flexibility are security oriented such as buffer overflows and string formatting and that is a reason why "plug-ins" libraries like The Safe C Library were designed to patch/correct.

    Actually quoting the Safe C String Library overt security goals "The API should be capable of tracking whether strings are "trusted", a la Perl's taint mode.";even they recognize Perl's value!

    This kind of quest for speed would us lead back to assembly which is faster than C. The question is would you like to deal with assembly? as C is a higher level that assembly, Perl is higher level than C.

    It' more a question of what you want to TRADE for speed. The industry is constantly trading speed for flexibility and safety. That is why the trend is oriented on Virtual Machines like Java Virtual Machine or the .NET CLR which are layers on top of the OS and protect you from those kind of issues.

    But if one wanted to find out what could be done to bring Perl closer to C he could take a look at Why Not Translate Perl to C? by Mark-Jason Dominus.

Re^2: compiling perl scripts aka why is perl not as fast as C
by Anonymous Monk on Aug 22, 2012 at 15:13 UTC
    The comparison would be better to C++, which is more similar to perl in "high-level" functionality (like adding strings) -- so what (if any) are the factors that prevent perl being compiled into a form that runs as fast as compiled C++?