On Wed, 7 Jan 2015 10:59:07 +0200 Shlomi Fish <shlo...@shlomifish.org> wrote: > Anyway, one can use the Benchmark.pm module to determine which > alternative is faster, but I suspect their speeds are not going to be > that much different. See: > > http://perl-begin.org/topics/optimising-and-profiling/ > > (Note: perl-begin.org is a site I originated and maintain).
And this is the answer I'd give - if you're curious as to which of two approaches will be faster, benchmark it and find out. It's often better to do this yourself, as the results may in some cases vary widely depending on the system you're running it on, the perl version, how Perl was built, etc. The sure-fire way to see which of multiple options is faster is to use Benchmark.pm to try them and find out :) For an example, I used the following (dirty) short script to set up 1,000 test filenames with random lengths and capitalisation, half of which should match the pattern, and testing each approach against all of those test filenames, 10,000 times: [davidp@supernova:~]$ cat tmp/benchmark_lc.pl #!/usr/bin/perl use strict; use Benchmark; # Put together an array of various test strings, with random # lengths and case my @valid_chars = ('a'..'z', 'A'..'Z'); my @test_data = map { join('', map { $valid_chars[int rand @valid_chars] } 1..rand(10)) . (rand > 0.5 ? '.bat' : '.bar') } (1..1000); Benchmark::cmpthese(10_000, { lc_first => sub { for my $string (@test_data) { $string = lc $string; if ($string =~ /\.bat$/) { } } }, regex_nocase => sub { for my $string (@test_data) { if ($string =~ /\.bat$/i) { } } }, }, ); And my results suggest that, for me, using lc() on the string first before attempting to match was around 30% faster: [davidp@supernova:~]$ perl tmp/benchmark_lc.pl Rate regex_nocase lc_first regex_nocase 2674/s -- -24% lc_first 3509/s 31% -- Of course, YMMV. -- David Precious ("bigpresh") <dav...@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/