Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Comment on Performace calculation
Select or Download Code
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% --

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-10-23 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls