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

Performace calculation

by anniyan (Monk)
on Nov 23, 2005 at 12:11 UTC ( #511063=perlquestion: print w/replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

Monks, now i am doing a small tool, in that i need to split the string into two, i want to know among the two methods which is the best method, else is there any other best way to do this?

I come up with this question because in our community often there is a discussion regarding the performance. Often i use to see the nodes saying dont call the regex engine unnecessarily?

If there is some conclusion out of this i will follow that in the future.

method1: $item = 'path/path1==new'; my ($epath, $relt) = split /==/, $item;
method2: $item = 'path/path1==new'; my ($epath, $relt) = $item =~ /([^=]*)==(.*)/;

Please apologise if it is silly.


Replies are listed 'Best First'.
Re: Performace calculation
by Perl Mouse (Chaplain) on Nov 23, 2005 at 12:21 UTC
    Unless you are doing this thousands of times, it will hardly matter which one is faster.

    If you must know, you can always use the Benchmark module, and run a simple test:

    #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; our $item = '"path/path1==new'; cmpthese(-1, { method1 => 'my ($epath, $relt) = split /==/, $item', method2 => 'my ($epath, $relt) = $item =~ /([^=]*)==(.*)/', }); __END__ Rate method2 method1 method2 334042/s -- -38% method1 535924/s 60% --
    Method 1 will about 1.1 microseconds faster (at least, on my box). So, if I have a million of them, method1 will gain 1 second over method2. That's hardly worth the trouble.

    I'd pick method1 because, IMO, it's more clear to see what it does.

    Oh, and both methods use a regular expression anyway.

    Perl --((8:>*
Re: Performace calculation
by murugu (Curate) on Nov 23, 2005 at 12:37 UTC
    Hi anniyan,

    In both the cases you are using regexp.

    Method1 seems faster than method2.

    To benchmark the code, go for Benchmark module

    Here is the comparison....

    use strict; use warnings; use Benchmark qw(cmpthese); cmpthese(-1, {method1=>sub {my $item = 'path/path1==new';my ($epath, $relt) = spli +t /==/, $item;},method2=>sub {my $item = 'path/path1==new';my ($epath +, $relt) = $item =~ /([^=]*)==(.*)/;} } ); Rate method2 method1 method2 200037/s -- -37% method1 315836/s 58% --

    Murugesan Kandasamy
    use perl for(;;);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://511063]
Approved by Corion
[ambrus]: So that, or if you really want something agnostic to the loop, then use curl multi. Its interface is quite reasonable (unless you use an old version),
[ambrus]: though it's very C-like (serious use of varargs so easy to pass the wrong type), and there are some minor problems (busy loop because it doesn't use the same timer as your event loop, so you have to artificially delay timer callbacks by a small amount).

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2016-12-07 16:25 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (130 votes). Check out past polls.