Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Count byte/character occurrence (1/4)

by james28909 (Deacon)
on Apr 04, 2016 at 20:58 UTC ( [id://1159556]=note: print w/replies, xml ) Need Help??


in reply to Re: Count byte/character occurrence (1/4)
in thread Count byte/character occurrence (quickly)

Away for pc right now, but would something with a for loop and substr be faster? I dont necessarily have to unpack any bytes, do I?
  • Comment on Re^2: Count byte/character occurrence (1/4)

Replies are listed 'Best First'.
Re^3: Count byte/character occurrence (1/4)
by BrowserUk (Patriarch) on Apr 04, 2016 at 21:05 UTC
    would something with a for loop and substr be faster? I dont necessarily have to unpack any bytes, do I?

    That requires a call into C (substr) for every byte; where using unpack requires a single call for the entire string.

    The cardinal rule for optimising Perl code, is to get perl's built-ins to do as much of the work as you can.

    Using this loop:

    ++$seen[ ord chop $buf ] while length $buf;
    in place of the unpack loop is almost but not quite as fast.

    It trades 2 built-in calls per byte, against the cost of building the unpack return list on the stack, and loses by a hair.


    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-18 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found