Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: the '..' operator and decreasing values

by merlyn (Sage)
on Mar 04, 2005 at 22:31 UTC ( [id://436788]=note: print w/replies, xml ) Need Help??


in reply to Re: the '..' operator and decreasing values
in thread the '..' operator and decreasing values

Or just
for (reverse 0..10) { ... }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^3: the '..' operator and decreasing values
by ikegami (Patriarch) on Mar 04, 2005 at 22:33 UTC

    Let's hope the list isn't too long. for (0..x) is evaluated lazily, but I doubt that for (reverse x..0) is as well.

    On second thought, that would rarely matter.

        It seems perl586delta fooled lots of us. I did som testing and it turns out that reverse 0..100 will turn 0..100 to a list of 101 elements and traverse that list from the end.
        for ( reverse 0 .. 100_000_000) {};
        Wants to grab lots of memory and gets killed by my OS, without reverse it works nicely. Tried with perl5.8.6 and perl5.9.1.

        I was about to post a benchmark, then I noticed that you were referring to 5.8.6. So I decided that now would be a good time to upgrade. So I did - spent the last little bit compiling, testing, and installing perl 5.8.6. Then I realised that my benchmark wasn't actually testing for, just the creation and reversing of the list. So, comparing them, I'd have to say that something just went wrong.

        use strict; use Benchmark qw(cmpthese); cmpthese(-1, { forward => sub { for (0 .. 10) {}; }, backward => sub { for (reverse 0 .. 10) {}; }, } );
        Noting that "perl" is perl 5.8.6, and "perl5.8.5" is, obviously, perl 5.8.5, and the script is called "y" (because I'm lazy):
        $ perl y Rate backward forward backward 182237/s -- -13% forward 210436/s 15% -- $ perl5.8.5 y Rate backward forward backward 234216/s -- -9% forward 256000/s 9% --
        Two things - first off, the difference between perl 5.8.6 and 5.8.5 is more significant than the difference between forward and backward, and the difference between forward and backward is actually worse. Granted, this is just a lazy list of numbers, not an actual array of scalars, but it's still somewhat disconcerting. Perhaps the P5P team put in a premature optimisation? ;-)

        Note that I reran the test with this code:

        use strict; use Benchmark qw(cmpthese); my @a = 0..100; cmpthese(-1, { forward => sub { for (@a) {}; }, backward => sub { for (reverse @a) {}; }, } );
        Again, perl 5.8.5 is faster than 5.8.6 here:
        $ perl y Rate backward forward backward 39822/s -- -12% forward 45189/s 13% -- $ perl5.8.5 y Rate backward forward backward 44799/s -- -3% forward 46089/s 3% --
        I would have expected, though, that perl 5.8.6 would have statistically-insignficant difference in speed between forward and backward iteration. That does not seem to be the case - 5.8.6 made the difference in speed worse vs 5.8.5.

        This is all based on a valid benchmark test - which is why I post the code. Anyone have anything that I might be missing, please tell me.

        Both 5.8.5 and 5.8.6 were compiled on the same OS, with nearly every option set to whatever default Configure sets them to, and the exception is something they both share: install path. Only difference I can think of is that 5.8.6 has a longer @INC, as it includes the 5.8.5 directories, but I don't see how that would play into this benchmark.

Re^3: the '..' operator and decreasing values
by thor (Priest) on Mar 04, 2005 at 22:50 UTC
    Does this create a list all at once so that it can reverse it as opposed ikegami's solution which creates only one value at a time? I ask because I honestly don't know.

    Update: Yarr...that's what I get for not refreshing.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found