Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: perl process slower and slower when loop number increase

by Dallaylaen (Chaplain)
on Jan 22, 2018 at 12:02 UTC ( [id://1207660]=note: print w/replies, xml ) Need Help??


in reply to perl process slower and slower when loop number increase

The total execution time is startup_time + count * iteration_time. For numbers like 10..10000 the loop time is negligible compared to reading the interpreter from disk, parsing script etc.

You can eliminate the startup time (assuming it's constant) by running several different big (10**6+) loops, then plotting time against count:

bash$ for i in `seq 10`; do echo $i; time perl -e 'for($i=0;$i<='$i'*1 +000000 ;$i++){}'; done 1 real 0m0.055s user 0m0.048s sys 0m0.000s 2 real 0m0.051s user 0m0.048s sys 0m0.000s 3 real 0m0.074s user 0m0.068s sys 0m0.000s 4 real 0m0.099s user 0m0.088s sys 0m0.004s 5 real 0m0.124s user 0m0.116s sys 0m0.000s 6 real 0m0.146s user 0m0.140s sys 0m0.000s 7 real 0m0.166s user 0m0.164s sys 0m0.000s 8 real 0m0.197s user 0m0.184s sys 0m0.000s 9 real 0m0.217s user 0m0.204s sys 0m0.004s 10 real 0m0.235s user 0m0.232s sys 0m0.000s bash$ for i in `seq 10`; do echo $i; time php -r 'for($i=0;$i<='$i'*10 +00000 ;$i++){}'; done 1 real 0m0.034s user 0m0.020s sys 0m0.012s 2 real 0m0.036s user 0m0.028s sys 0m0.000s 3 real 0m0.035s user 0m0.032s sys 0m0.000s 4 real 0m0.040s user 0m0.036s sys 0m0.000s 5 real 0m0.048s user 0m0.040s sys 0m0.004s 6 real 0m0.055s user 0m0.052s sys 0m0.000s 7 real 0m0.065s user 0m0.060s sys 0m0.000s 8 real 0m0.071s user 0m0.068s sys 0m0.000s 9 real 0m0.080s user 0m0.076s sys 0m0.000s 10 real 0m0.087s user 0m0.084s sys 0m0.000s

What surprises me here is that PHP is much faster than Perl.

Replies are listed 'Best First'.
Re^2: perl process slower and slower when loop number increase
by hippo (Bishop) on Jan 22, 2018 at 12:15 UTC
    What surprises me here is that PHP is much faster than Perl.

    It's no surprise to me. I always suspected PHP would be really fantastic at doing something utterly useless. ;-)

Re^2: perl process slower and slower when loop number increase
by karlgoethebier (Abbot) on Jan 22, 2018 at 12:09 UTC
    "...PHP is much faster than Perl"

    I guess this must be a measurement error ;-)

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^2: perl process slower and slower when loop number increase
by Anonymous Monk on Jan 22, 2018 at 13:18 UTC
    not just php,

    xxxx vs perl have same behaviour.

    this loop test is maybe "useless" and "doing nothing" but why only perl slowing down for "do nothing" until "certain number",

    and for "real useful app", try to parse 1M line of single file, perl already faster from other for read, match with regex, text processing, etc, but this "loop number" is the problem

    the question is still: can that "certain number" be set ?

    try: time node -e 'for($i=0;$i<=1000000;$i++){}' or use java, c, etc for "doing nothing" they not slowing down, only perl does
      Bear in mind that C, Java etc are compiled languages. The compiler can take as long as it likes to optimise code, and is likely to detect an empty loop and optimise it away. Perl does its compilation as part of its execution, and can therefore only spend a limited amount of time applying optimisations. In particular, it won't optimise away a for(;;) loop with an empty body. So the timings you see for perl are a fixed compilation overhead, followed by N executions of a sequence of opcodes which collectively increment $i, test it against a limiting value, and jump back.

      You haven't shown anything apart from perl taking a time linearly proportional the the number of times the loop is executed. It's not clear to me why you find that surprising.

      Dave.

        >> Bear in mind that C, Java etc are compiled languages

        AFAIK, C is compiled language, Java etc only "compile" to intermediate code, well, AFAIK (again) perl have optimizer that do like java, but no need create intermediate code file like java class

        php and nodejs are not "compiled" language, but their loop process not slowing down

        >> You haven't shown anything apart from perl taking a time linearly proportional the the number of times the loop is executed

        in my vps box: $ time perl -e '' real 0m0.003s user 0m0.000s sys 0m0.000s $ time perl -e 'for($i=0;$i<=0;$i++){}' real 0m0.003s user 0m0.000s sys 0m0.000s $ time perl -e 'for($i=0;$i<=10;$i++){}' real 0m0.004s user 0m0.004s sys 0m0.000s $ time perl -e 'for($i=0;$i<=100;$i++){}' real 0m0.004s user 0m0.000s sys 0m0.000s $ time perl -e 'for($i=0;$i<=1000;$i++){}' real 0m0.003s user 0m0.000s sys 0m0.000s $ time perl -e 'for($i=0;$i<=10000;$i++){}' real 0m0.005s user 0m0.004s sys 0m0.000s $ time perl -e 'for($i=0;$i<=100000;$i++){}' real 0m0.015s user 0m0.012s sys 0m0.000s $ time perl -e 'for($i=0;$i<=1000000;$i++){}' real 0m0.094s user 0m0.088s sys 0m0.004s $ time perl -e 'for($i=0;$i<=10000000;$i++){}' real 0m0.731s user 0m0.724s sys 0m0.004s $ time perl -e 'for($i=0;$i<=100000000;$i++){}' real 0m7.253s user 0m7.256s sys 0m0.000s $ time perl -e 'for($i=0;$i<=1000000000;$i++){}' real 1m13.921s user 1m13.828s sys 0m0.008s

        I still think there limit number between $i=100000 and $i=1000000;

        but where to set it ?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found