Re: There's no LIMIT to my SPLITting headache.



Mark Seger wrote:

> The most enlightening experience I've had with perl was discovering that:
>
> statement if $a=~/aaa|bbb|ccc/
>
> is a LOT slower than
>
> statement if $a=~/aaa/ || $a=~/bbb/ || $a=~/ccc/
>
> by a factor of about 7, at least when I checked it out on both a 3.5GHz
> Xeon and a 1.5GHz Intanium 2. The moral to the story is nothing beats a
> few simple tests.

Not by a long shot on my machine, running Perl 5.8.7.

=code

use Benchmark qw(:hireswallclock cmpthese);

my $pat = 'A Perl Paaattern';
my $match = 0;

cmpthese(0, {
'Code1' => '$match = 1 if $pat =~/aaa/ || $pat=~/bbb/ || $pat=~/ccc/',
'Code2' => '$match = 1 if $pat =~/aaa|bbb|ccc/',
});

=cut

Time::HiRes is compiled on my machine.

Granted, that's a *very* simple pattern, but the shorter code came out
ahead 5/5 times by a wide margin.

___________________________
Rate Code1 Code2
___________________________
Code1 2775928/s -- -62%
Code2 7368521/s 165% --
___________________________
Code1 2888905/s -- -63%
Code2 7777375/s 169% --
___________________________
Code2 2647141/s -- -64%
Code1 7289831/s 175% --
___________________________
Code1 2742926/s -- -62%
Code2 7313196/s 167% --
___________________________
Code1 2803761/s -- -61%
Code2 7211258/s 157% --
---------------------------

Or if I set $pat to a string of the output from 'perldoc Time::HiRes'

Code1 2657898/s -- -70%
Code2 8771367/s 230% --


Dave


.



Relevant Pages

  • Re: lies about OOP
    ... He thinks that OOP has more overhead and is slower than ... Perl) which is compiled to a bytecode which is then interpreted.... ...
    (comp.lang.python)
  • Re: Perl vs. Python for text manipulation
    ... slower than perl for text manipulation simply because of this IO ... post process it in python to get acceptable performance. ... The perl "built-in" idioms for text processing is not only a syntactic ... Ganesan R ...
    (comp.lang.python)
  • Re: Slow script
    ... works in a specific environment. ... I did not expect folks here to pinpoint ... >>I have a script I inherited that was developed under perl 5.004. ... It runs much much slower. ...
    (comp.lang.perl.misc)
  • Re: Math
    ... I realize that any math in Perl is probably slower than the same math ... In my experience, perl is generally slower for heavy number crunching, ... blb8 at po dot cwru dot edu ...
    (comp.lang.perl.misc)
  • Re: Problems passing a reference to a hash between functions
    ... M> But as noticed Paul Lalli, we're programming Perl;) ... M> While shorter code is more readable and powerful:) ...
    (comp.lang.perl.misc)