EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, are there any packages I can get such that I can convert my perl code into C. I need my code to go as fast as possible.

Replies are listed 'Best First'.
Re: Packages to make perl code go faster?
by dragonchild (Archbishop) on Feb 14, 2005 at 15:14 UTC
    Inline::C is a good place to start. You won't be able to convert the code wholesale, but you can rewrite some sections of your code using C, if you need to.

    Now, have you actually identified the sections of your code that are causing any slowdowns? Over 90% of all time taken in the average application is spent in less than 10% of the code. You might want to look at various profiles, like Devel::DProf and Devel::SmallProf.

    Another important item is you say I need my code to go as fast as possible. What does this mean? The human brain can only notice things that take longer than a hundredth of a second (or so). What does it gain you if you speed your code up by 50%, but only shave a thousandth of a second off the runtime?

    When I code web applications, I try and keep each page under 5 seconds from request to render. That's the time that most people feel a webpage should render in. Client/server applications generally need to respond in under 1-2 seconds. Cellphone applications that go to the network can take up to 30 seconds and still be reasonable. In other words - how fast does fast really need to be?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Packages to make perl code go faster?
by borisz (Canon) on Feb 14, 2005 at 15:16 UTC
    Compile your perl interpreter without (i)threads support.
    Boris
Re: Packages to make perl code go faster?
by gaal (Parson) on Feb 14, 2005 at 15:36 UTC
    borisz++

    If you aren't using Unicode, specify -C0 on the shebang line. Character conversions are very expensive.

Re: Packages to make perl code go faster?
by davido (Cardinal) on Feb 14, 2005 at 16:27 UTC

    Many times upon profiling the code as recommended by dragonchild, you'll find that just a few segments of code are your bottlenecks. In some instances, the only solution is to code them in C, but more often than not, rethinking the design can save you a lot of execution time without dropping into C.

    For example, hash lookups are faster by a long shot than grepping a list, if the dataset is large. Maybe nested foreach loops can be replaced by a smarter algorithm that doesn't require so much nested looping. Perhaps database inserts can be done in larger transactions, only committing after 1000 rows, instead of committing individual rows. The list goes on and on. Before looking for a lower level language, first look to see if you're getting the most out of your higher level language. An inefficient algorithm written in Perl will still be inefficient in C.


    Dave

Re: Packages to make perl code go faster?
by Taulmarill (Deacon) on Feb 14, 2005 at 15:12 UTC
    you can _replace_ slow subroutines using Inline::C, but a Perl2C converter is nothing you realy want to use. they just don't work.
Re: Packages to make perl code go faster?
by Fletch (Bishop) on Feb 14, 2005 at 15:13 UTC
    perldoc -q "compile my Perl"
Re: Packages to make perl code go faster?
by Anonymous Monk on Feb 14, 2005 at 16:15 UTC
    If there were such packages, that is, packages that will make Perl code faster that it is going now, regardless of the code, wouldn't you think it was already integrated in the core? More specifically, if it were feasible to compile Perl code to C to get a significant speedup, wouldn't you think it was already part of the core? Or at least, mentioned somewhere in the FAQ?

    If you need a general speedup, here are some boosters:

    • Use a better algorithm.
    • Ditch Perl, write it in C.
    • Shutdown anything else that's running on the machine.
    • Invest in hardware: more memory, faster disks, more I/O controllers (and more spindles), different RAID scheme, faster CPU, faster network, more network cards.
    But there isn't a magic bullet you can download.
Re: Packages to make perl code go faster?
by hawtin (Prior) on Feb 15, 2005 at 11:37 UTC

    There are many things that can slow your script down. As others have said usually 5% of the code consumes 90% of the execution time. The trick is to identify where those key routines are and look at ways to speed them up.

    I have used tricks like caching results that can have a significant effect:

    sub routine1 { my($arg1,$arg2) = @_; ... return $result; }

    Is changed to:

    { my %cached_results; sub routine1 { my($arg1,$arg2) = @_; return $cached_results{"$arg1||$arg2"} if(defined $cached_results{"$arg1||$arg2"}); ... $cached_results{"$arg1||$arg2"} = $result; return $result; } sub clear_cache1 { %cached_results = (); } }

    I recently had a case where I was using .= to build up a string, converting the routine to pushing strings on to and array and only concatenating at the end of the function reduced memory usage by a factor of 100 and increased speed by a factor of 10 for that one routine.

    It is not worth hand coding modules in C until you have tried these types of tricks. The extra maintenance cost will swamp the benefit of speed you get.