in reply to Re^2: Performance optimization question
in thread Performance optimization question
#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(time); my $num = 3_000_000; my $str = 'x'x$num . 'reg exp' . 'x'x$num; my $start = time; my $res = $str =~ m/reg exp/; print time - $start, $/; $start = time; my $idx = index $str, 'reg exp'; print time - $start, $/; __END__ 0.0114099979400635 0.011760950088501
Admittedly, index is still a bit slower, but the difference isn't that huge.
BTW on my machine (with perl 5.8.8) the difference isn't there at all:
Rate REGEX INDEX REGEX 87061/s -- -2% INDEX 88494/s 2% --
The results only differ slightly for 5.10.0. Which perl did you use?
I thought that index and regexes use the same algorithm, but the regex goes through the pain of compiling the regex first
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Performance optimization question
by BrowserUk (Patriarch) on Apr 03, 2008 at 08:09 UTC |