Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

After reading the other remarks here and (wildly) guessing that your string invocation and copying may look not look realistic for tasks usually done with character substitutions, I tried to make the test more illustrative - (re-ordered your code and added some meaningful names ;-)

The output is subsequently generated for different string sizes, from 2x10^1 to 2x10^4 bytes of length:

use strict; # Purpose: In each benchmark invocatio +n, have one use warnings; # (constant) string copied to another- + which is then # modified (shortened by the first cha +racter) and for my $n (1..4) { # touched again (length determined and + compared) use Benchmark qw(cmpthese); my $org_str = '|0' x 10**$n; # generate the string in local scope my $mod_str = $org_str; # do some allocation on the other stri +ng's PV print "string length: " . length($org_str) . "\n"; cmpthese( -3, { regexsubst => sub { # copy and modify ($mod_str = $org_str) =~ s/.//; die unless length($mod_str)+1 == length($org_str +) }, substr_rhs => sub { # there's no point full string copy, simply c +opy what's needed $mod_str = substr($org_str, 1); die unless length($mod_str)+1 == length($org_str +) }, substr_lhs => sub { # copy and modify substr($mod_str = $org_str, 0, 1) = ''; die unless length($mod_str)+1 == length($org_str +) }, reversestr => sub { # reverse, copy, modify, reverse chop($mod_str = reverse($org_str)); $mod_str = reverse $mod_str; die unless length($mod_str)+1 == length($org_str +) } } ); print '- ' x 30, "\n" }

On my machine (5.10), the right-side substr() wins almost always (if the string in question is not longer than some KB), as the reverse-chop-reverse looses. The regex-subst performance approaches the substr speed asymptotically as the string gets longer - but seems to be slower as the left-side-substr() on the shortest string tested.

The funny part is: the left-side substr() will beat the right side substr() if the string exceeds some (larger) size. I wouldn't have thought of this one!

Results:

string length: 20 Rate reversestr substr_lhs regexsubst substr_rhs reversestr 1120993/s -- -12% -13% -48% substr_lhs 1273691/s 14% -- -1% -41% regexsubst 1289977/s 15% 1% -- -40% substr_rhs 2144546/s 91% 68% 66% -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string length: 200 Rate reversestr regexsubst substr_lhs substr_rhs reversestr 741492/s -- -37% -39% -61% regexsubst 1172942/s 58% -- -3% -39% substr_lhs 1211322/s 63% 3% -- -37% substr_rhs 1915850/s 158% 63% 58% -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string length: 2000 Rate reversestr substr_lhs regexsubst substr_rhs reversestr 145793/s -- -82% -82% -84% substr_lhs 795724/s 446% -- -4% -11% regexsubst 828896/s 469% 4% -- -7% substr_rhs 894365/s 513% 12% 8% -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string length: 20000 Rate reversestr substr_rhs regexsubst substr_lhs reversestr 16427/s -- -86% -92% -92% substr_rhs 119927/s 630% -- -39% -41% regexsubst 197394/s 1102% 65% -- -3% substr_lhs 202514/s 1133% 69% 3% -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Update:

After studying Moritz' Code (there is one - but hidden behind readmore-tags ;-), the results are compatible (he tested long strings too), but Moritz wasn't curious about it - so it might come as expected.

Apparently, there seems to be no 'flip-flop', as hsmyers suggested - the left-side substr() simply takes over on longer strings (above several KB).

my € 0.02

mwa


In reply to Re: Ways to delete start of string by mwah
in thread Ways to delete start of string by hsmyers

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: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found