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

Re: Alternative to bytes::length()

by Anonymous Monk
on Dec 23, 2009 at 01:44 UTC ( [id://814039]=note: print w/replies, xml ) Need Help??


in reply to Alternative to bytes::length()

Why? Sounds premature to me
#!/usr/bin/perl -- use strict; use warnings; use Benchmark qw( cmpthese ); # Make bytes:: functions available, but use character semantics. use bytes; no bytes; cmpthese( -3, { bytes => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while bytes::length($smileys); }, utf8 => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while length($smileys); }, substr => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while ord substr($smileys,0,1); }, notnot => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while !! $smileys; }, '!!bytes' => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while !! bytes::length($smileys); }, '!!utf8' => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while !! length $smileys; }, 'ne""' => sub { my $smileys = "\x{263a}" x 10_000; chop($smileys) while $smileys ne ""; }, } ); __END__ Rate substr utf8 !!utf8 bytes !!bytes ne"" notnot substr 3.75/s -- -3% -4% -97% -97% -99% -99% utf8 3.86/s 3% -- -1% -97% -97% -99% -99% !!utf8 3.90/s 4% 1% -- -97% -97% -99% -99% bytes 119/s 3066% 2972% 2942% -- -0% -71% -74% !!bytes 119/s 3081% 2987% 2956% 0% -- -71% -74% ne"" 406/s 10740% 10419% 10314% 242% 241% -- -10% notnot 451/s 11928% 11572% 11455% 280% 278% 11% --

Replies are listed 'Best First'.
Re^2: Alternative to bytes::length()
by creamygoodness (Curate) on Dec 23, 2009 at 01:54 UTC

    Seems like ne "" is what I was looking for. :)

    The !! construct won't work because certain strings with lengths can be false:

    marvin@smokey:~ $ perl -le 'print "true" if !!"0";' marvin@smokey:~ $ perl -le 'print "true" if !!"1";' true

    What did you mean by "premature", though?

      Seems like ne "" is what I was looking for. :)

      Be careful. Anonymonk's benchmark is conflating an aweful lot of other stuff in with the actual code you are concerned about.

      I believe (but I'm open to correction), this to be a far better benchmark, and it shows a radically different result. It might just set your mind at ease. (Or not!):

      #!/usr/bin/perl -- use strict; use warnings; use Benchmark qw( cmpthese ); # Make bytes:: functions available, but use character semantics. use bytes (); our $smileys = "\x{263a}" x 10_000; our $empty = "\x{263a}"; chop $empty; cmpthese -1, { bytes => q{ my $c=0; ( bytes::length($empty) or bytes::length($smileys) ) and ++$c +for 1 .. 1000; }, utf8 => q{ my $c=0; ( length($empty) or length($smileys) ) and ++$c for 1 .. 1000; }, ord => q{ my $c=0; ( ord( $empty ) or ord( $smileys ) ) and ++$c for 1 .. 1000; }, 'ne""' => q{ my $c=0; ( $empty ne '' or $smileys ne '' ) and ++$c for 1 .. 1000; }, }; __END__ C:\test>junk8 Rate bytes ord ne"" utf8 bytes 1379/s -- -72% -75% -76% ord 4992/s 262% -- -10% -13% ne"" 5566/s 304% 12% -- -3% utf8 5757/s 317% 15% 3% --

      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.

        I believe that your 'utf8' case is mostly benchmarking the pulling out of the character count cached in the magic, thus completely missing the original problem. Even if it weren't, I don't see how your benchmark provides any justification for not using eq '' (which perhaps you weren't trying to imply).

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-18 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found