If it's of any interest. I went back to first principles and came up with this which verified correct and was about 12% quicker than your original.

@items = grep { if (!exists $del{$_}) { $i++; 1; } else { $index -= 1 if $i <= $index; 0 ; } } @items;

Then I realised that by pre-incrementing $i, it would never be zero so I could drop the 1;.

That the -= 1 if ($i <=$index) was identical to -=($i <= $index);

The 'then' part of the if could then be replaced with and and the else> with or.

The result is the improvement of +20% performance.

Whether the reduction in readability is worth it is a matter of choice and the application. If the list is big and the deletions frequent and it was running as part of a CGI for instance, it probably is. Other situations you have to judge on a case by case basis.

Personally, I would rather see 3 lines of comment explaining one efficient, (correct:) line of code, than 3 lines of self documenting code, but that is very much a personal preference.


Well It's better than the Abottoire, but Yorkshire!

In reply to Re: Re: Re: Re: Re: round-robin on varying sequence by BrowserUk
in thread round-robin on varying sequence by dpuu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.