Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Ways to delete start of string

by hsmyers (Canon)
on May 27, 2008 at 17:12 UTC ( #688701=note: print w/replies, xml ) Need Help??


in reply to Re^2: Ways to delete start of string
in thread Ways to delete start of string

'purport'! You are suggesting that I'm not testing? Or is this one of those pond translation errors? Must be since I'm clearly posting results. Good point about strict and warnings They are an artifact of a 'new' file in my editor. Could you give an example of 'add a multiplier loop inside the subroutine'? While I still insist that I'm looking for better ways to pre-chop, learning about benchmarking is both fascinating and useful.

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re^4: Ways to delete start of string
by kyle (Abbot) on May 27, 2008 at 17:36 UTC

    For an example of multiplier loops inside the subroutines, have a look at Re: Bug or WAD in lvalue substr? (again.).

    If you call a sub in a loop, you are timing how long it takes to call the sub as well as the time it takes to execute the contents of the sub. Often this is not an issue since the contents of the sub take a lot longer than just calling the sub itself. If the loop is inside the sub instead, then you can focus more on the contents of the loop rather than the time it takes to call subs, which is important when you're timing something that's as fast as sub calls.

    For more on benchmarking in general, have a look at the "Benchmarking Perl" chapter in Mastering Perl.

      If you call a sub in a loop, you are timing how long it takes to call the sub as well as the time it takes to execute the contents of the sub.

      Note that Benchmark goes to some lengths to try to subtract out the time taken by the "calling the sub" part. Of course, these attempts can certainly be unsuccessful (to the point of Benchmark declaring defeat or just to the point of Benchmark producing meaningless results; though I think that interpretation is often valid even when one has worked around the issue as you described).

      which is important when you're timing something that's as fast as sub calls

      There is very little that I would characterize as "important" when trying to time something in Perl that is about as fast as a sub call. Well, other than the idea of "you are likely wasting your time". :)

      - tye        

Re^4: Ways to delete start of string
by BrowserUk (Pope) on May 27, 2008 at 18:39 UTC
    'purport'! You are suggesting that I'm not testing?

    No. I should have said: "That your benchmark purports to be testing". (Where 'your' refered to ikegami). The point is that it looks like it's testing the right thing, but when you factor in the overhead of the tests, it serves to completely obscure the results.

    Like others, I think this is a fairly fruitless test. If you are doing this once, then the method used will make very little difference. If you are doing it hundreds of thousands of times, then there are far more efficient ways of doing it. For example,

    1. if the idea is to chip off characters until you reach a particular character, then search for the character and then chop the lump off:
      ## Either substr( $x, 0, index( $x, $char ), ''); ## or $x =~ s[(^/*$char)][];
    2. if the requirement is to remove and process the first N chars of a string, then chop off the lump then separate them:
      for my $char ( unpack '(A1)*', substr $x, 0, 100, '' ) { ## }

    The only time the relative performance of these methods is likely to make a significant difference is if you were applying it once to each of a large number of strings, say a large array, much as you might with chop( @array ). And it would have to be in the order of 10e7 elements before it would have any significant effect upon an application. If it was a common requrement, then there would probably be a leading character equivalent of chop (chip()?:) built-in.

    Could you give an example of 'add a multiplier loop inside the subroutine'?

    This was how I constructed my variation:

    use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( subst => '$x =~ s/.//;', substr_lval => 'substr($x,0,1) = "";', substr_mod => 'substr($x,0,1,"");', reverse => '$x = reverse $x; chop($x); $x = reverse($x);', substr_copy => '$x = substr($x,1);', ); our $loops ||= 1e4; for (values %tests) { $_ = <<EOT my \$x = 'X' x $loops; for( 1 .. $loops ) { $_ } EOT } cmpthese(-3, \%tests);

    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 agree that there is little to be gained in such an optimization. That said, I've made out like a bandit at the 'meta' level by posting the article itself. Not the least of which are the pieces you've posted. Much thanks. Please note that I did not start my usual rant about how optimization is always a good thing. Be grateful.

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-12-11 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?