>>
>> Why slow it down with the 'i'?
>>   $searchstring=~/[a-zA-Z0-9]/;

> Why slow it down with a regular expression?  :-)

> if ( $searchstring =~ tr/a-zA-Z0-9/ ) {


i was bored, and thought i'd try a little benchmark. note, this
is for the specific problem expressed here, not the general case.

i benchmarked the following:

  RegexWithI: $string =~ /[A-Z0-9]/i;
  RegexNoI:   $string =~ /A-Za-z0-9/;
  tr:         $string =~ tr/[A-Za-z0-9//;

i tested each with $string eq "A" and $string eq "z"

i ran it three times on my perl 5.6.1, linux 2.4.7, celeron 400
laptop. the results (formatting slightly adjusted)

Benchmark: timing 1000000 iterations
  RegexNoI(A): 18 wallclock secs (14.05 usr +  0.38 sys = 14.43 CPU) @ 69300.07/s 
(n=1000000)
  RegexNoI(z): 17 wallclock secs (14.05 usr +  0.40 sys = 14.45 CPU) @ 69204.15/s 
(n=1000000)
RegexWithI(A): 16 wallclock secs (13.86 usr +  0.40 sys = 14.26 CPU) @ 70126.23/s 
(n=1000000)
RegexWithI(z): 18 wallclock secs (13.54 usr +  0.25 sys = 13.79 CPU) @ 72516.32/s 
(n=1000000)
        tr(A): 12 wallclock secs (11.30 usr +  0.16 sys = 11.46 CPU) @ 87260.03/s 
(n=1000000)
        tr(z): 16 wallclock secs (11.32 usr +  0.50 sys = 11.82 CPU) @ 84602.37/s 
(n=1000000)

Benchmark: timing 1000000 iterations
  RegexNoI(A): 16 wallclock secs (13.84 usr +  0.29 sys = 14.13 CPU) @ 70771.41/s 
(n=1000000)
  RegexNoI(z): 17 wallclock secs (14.05 usr +  0.33 sys = 14.38 CPU) @ 69541.03/s 
(n=1000000)
RegexWithI(A): 16 wallclock secs (13.57 usr +  0.27 sys = 13.84 CPU) @ 72254.34/s 
(n=1000000)
RegexWithI(z): 20 wallclock secs (14.06 usr +  0.53 sys = 14.59 CPU) @ 68540.10/s 
(n=1000000)
        tr(A): 15 wallclock secs (11.19 usr +  0.25 sys = 11.44 CPU) @ 87412.59/s 
(n=1000000)
        tr(z): 13 wallclock secs (11.17 usr +  0.14 sys = 11.31 CPU) @ 88417.33/s 
(n=1000000)

Benchmark: timing 1000000 iterations
  RegexNoI(A): 17 wallclock secs (13.76 usr +  0.16 sys = 13.92 CPU) @ 71839.08/s 
(n=1000000)
  RegexNoI(z): 18 wallclock secs (13.95 usr +  0.41 sys = 14.36 CPU) @ 69637.88/s 
(n=1000000)
RegexWithI(A): 15 wallclock secs (13.69 usr +  0.27 sys = 13.96 CPU) @ 71633.24/s 
(n=1000000)
RegexWithI(z): 14 wallclock secs (13.49 usr +  0.06 sys = 13.55 CPU) @ 73800.74/s 
(n=1000000)
        tr(A): 13 wallclock secs (11.17 usr +  0.16 sys = 11.33 CPU) @ 88261.25/s 
(n=1000000)
        tr(z): 13 wallclock secs (10.78 usr +  0.25 sys = 11.03 CPU) @ 90661.83/s 
(n=1000000)


make if that what you will...

i've pasted the code i used below, it isn't very interesting.

-- 
Best regards, Daniel

"The only thing worse than being talked about - is not being talked about!" -
Oscar Wilde.




#!/usr/bin/perl -w


use Benchmark;

$string1 = "A";
$string2 = "z";

timethese( 1000000, { 'RegexWithI(A)' => '&with_i($string1)' ,
                    'RegexWithI(z)' => '&with_i($string2)',
                    'RegexNoI(A)' => '&without_i($string1)',
                    'RegexNoI(z)' => '&without_i($string2)',
                    'tr(A)' => '&tr_without($string1)',
                    'tr(z)' => '&tr_without($string2)'
                    
                  });

sub with_i {
        my ($string) = @_;

        $string =~ /[A-Z0-9]/i;
}

sub without_i {
        my ($string) = @_;

        $string =~ /[A-Za-z0-9]/;
}

sub tr_without {
        my ($string) = @_;

        $string =~ tr/[A-Za-z0-9]//;
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to