Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Teach him a lesson with facts

by derby (Abbot)
on Feb 22, 2003 at 12:38 UTC ( #237720=note: print w/replies, xml ) Need Help??

in reply to Teach him a lesson with facts

I did write the code for all three languages and I did run them. I didn't post the code because 1) the poster was correct about the timing and 2) as others in the thread pointed out, it is a very contrived and useless test case for language comparison. Why does the Java and C code run faster? Loop optimization. The java compiler and c compiler I used optimized the loop away so the execution time is O(1). Perl did not optimize the loop away. Could it have? I'm not sure if there are any command line switches to do this type of optimization. Should that be the basis to throw away the whole language? No. Pure execution speed should be just one of your criteria and a very low one at that (otherwise we'd all be programming in machine language).


Replies are listed 'Best First'.
Re: Re: Teach him a lesson with facts
by theorbtwo (Prior) on Feb 22, 2003 at 12:49 UTC

    Perl is very difficult to optimize. For example, take the code

    for (0..1_000_000) { $x=$_+1; }
    (Take my code, please!) In C, the equivlent code would be equivlent to x=1000001. In perl, you can't do that optimization, because $x might be tied, in which case, writing to it might have side effects. In C, you'd have to declare that, using the volitile keyword. In perl, there's no way to tell, so the compiler can't assume anything.
    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Re: Teach him a lesson with facts
by Anonymous Monk on Feb 22, 2003 at 18:10 UTC
    So I was right about that testing.

    Now for whether that testing is meaningful. I tell what, it is meaningful. My purpose is not to show you that Perl cannot do ++ quick. What's wrong with perl is that it cannot do loop quick, as the first reply to your post agreed.

    This is something serious, loop is something you used everywhether in your real program, not a testing case made up by me.

    This is just a tip of the iceburger. I don't want argue about this any more. If you say this is the only performance issue in Perl, it is fine with me.

      I think you're misunderstanding how optimization works. Take the code posted above:

      for (0..1_000_000) { $x=$_+1; }

      Or in C or Java:

      int i, x; for(i = 0; i < 1000000; i++) { x = i + 1; }

      C and Java optimizers can turn the above into this:

      int x = 1000000;

      Thus totally eliminating the loop. Run your C compiler with -O0, and you'll get comparible performance to the Perl version.

      Why isn't this test relevent? Because the C/Java optimizer cannot always unroll loops. This is fairly common in any real-world program.

      Reinvent a rounder wheel.

      Note: All code is untested, unless otherwise stated

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-10-18 05:20 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (99 votes). Check out past polls.