Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^5: Out of Memory

by BrowserUk (Pope)
on Mar 28, 2013 at 19:12 UTC ( #1026024=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Out of Memory
in thread Out of Memory

Are you sure this is the most efficient way to do this?

Yes, I am quite sure.

  1. tr doesn't make copies, it operates in-place.
  2. It is optimised internally to recognise that when the source and replacements are identical, it is in 'counting mode' only.

For multi-character substring counting avoid creating the huge list:

$s = 'ab'x 10e6;; ++$n while $s=~ m[ab]g;; print $n;; 10000000

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^5: Out of Memory
Download Code
Re^6: Out of Memory
by Michael Kittrell (Novice) on Mar 28, 2013 at 19:31 UTC
    Ah that makes sense, I was wondering if it would not replace if both were the same.

    how does

    ++$n while $s=~ m[ab]g;;

    differ in execution from

    while($_=~s\0g) {count++}

    Thanks for sharing

      how does ... differ in execution from ...

      Hm. "in execution"?

      1. The result -- the count -- is the same.
      2. The runtime is less than half:
        $s = 'ab'x 10e6;; cmpthese -1, { a => q[ my $n = 0; while( $s =~ m[ab]g ){ $n++ }; ], b=>q[ my $n = 0; ++$n while $s =~ m[ab]g; ], };; s/iter a b a 2.31 -- -37% b 1.45 59% --
      3. It is easier to read.

      Don't use post increment if you aren't using the pre-increment value. Don't create scopes you don't need.

      Or, get into the habit of not doing anything you don't need to.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thank you for the very thorough explanation and proof.

        If you don't know where you're going, it doesn't matter when you get there.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-12 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls