Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^7: Alternative to bytes::length() (7% solution)

by BrowserUk (Patriarch)
on Dec 23, 2009 at 16:58 UTC ( [id://814128]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Alternative to bytes::length() (7% solution)
in thread Alternative to bytes::length()

A parser which consumes the string would be an example application.

Then It would be better to test for the success of the operation that consumes the string, than perform that operation and then test to see if there is any string left to consume. Simplistically:

cmpthese -1, { a=>q[ 1 while chop( $sa ) ], b=>q[ chop( $sb ) while $sb ne '' ] };; Rate b a b 8537447/s -- -59% a 20591583/s 141% --

But most other operations that alter (reduce) the length of a scalar can be tested in the same way.

That said, if there is any reason why the reducing operation itself cannot be used as the controlling condition, just testing the string for truth achieves the desired effect even more efficiently:

[0] Perl> cmpthese -1, { a=>q[ 1 while chop( $sa ) ], b=>q[ chop( $sb ) while $sb ne '' ], c=>q[ chop( $sc ) while $sc ] };; Rate b a c b 8686428/s -- -2% -20% a 8821000/s 2% -- -19% c 10841356/s 25% 23% --

Though I realise that can fail under some conditions.


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found