Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
no I didn't, it was just a guess :-) and in fact, it seems the double reverse solution is faster for small strings, while the substr solution wins in the long run. anyway, here you get full benchmark:
use strict; use List::Util qw(shuffle); use Benchmark qw(:all); my $string = "ACta"; print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string = join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); sub uclast_substr { my($string) = @_; $string = lc(substr($string, 0, length($string)-1)) . uc(substr($string, -1, 1)); return $string; } sub uclast_2reverse { my($string) = @_; $string = reverse ucfirst lc reverse $string; return $string; }
and here the result:
*** 4 characters
                    Rate   uclast_substr uclast_2reverse
uclast_substr   566572/s              --            -15%
uclast_2reverse 666223/s             18%              --
*** 52 characters
                    Rate   uclast_substr uclast_2reverse
uclast_substr   470588/s              --             -6%
uclast_2reverse 500000/s              6%              --
*** 208 characters
                    Rate uclast_2reverse   uclast_substr
uclast_2reverse 249938/s              --             -9%
uclast_substr   273523/s              9%              --
*** 676 characters
                    Rate uclast_2reverse   uclast_substr
uclast_2reverse 102239/s              --            -19%
uclast_substr   126231/s             23%              --
cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris


In reply to Re: Re: Re: Implement uclast with a regex by dada
in thread Implement uclast with a regex by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found