Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-03-21 09:21 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (265 votes). Check out past polls.