Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Out of Memory

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


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

The simplest, fastest and most efficient way to count the nulls (or any character) in a string is:

my $nulls = $string =~ tr[\0][\0];

Update: corrected '0' to '\0'. Thanks to davido.


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^3: Out of Memory
Download Code
Replies are listed 'Best First'.
Re^4: Out of Memory
by Michael Kittrell (Novice) on Mar 28, 2013 at 19:05 UTC
    Thanks... I didn see a reference to it in the thread that I got the while loop from but it noted that TR can only be used with single characters? which works in this case but maybe not others? I did try the TR thing and it does work for my data set without a memory error.

    Are you sure this is the most efficient way to do this? Seems like to me that its creating a copy of the original string and trying to replace the matches before it outputs the count. (as far as i can tell given reading a quick page on teh TR function) I wouldn't think that would be as memory efficient as the while code... but I don't understand the internals of the while code either, if it instantiates a huge list the interates through them, I can see how that wouldn't be as efficient as teh TR code.

      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.
        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-07-30 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls