in reply to Re^2: Fast Replacement (0.000025s)
in thread Fast Replacement
Looking again I see you're right.
But still, rather than invoking the regex engine 50,000 times, better to search for the position of the 50,000th ! and then replace in one pass.
#! perl -slw use strict; use Time::HiRes qw[ time ]; my $s = '1234!' x 55e3; my $start = time; my( $p, $c ) = ( 0, 50e3 ); 1 while --$c and $p = 1+ index $s, '!', $p; substr( $s, 0, $p ) =~ tr[!][\n]; printf "Took %f seconds\n", time() - $start; __END__ C:\test>junk71;; Took 0.011771 seconds C:\test>junk71;; Took 0.009690 seconds
That could probably be sped up with a binary chop for the position, but it hardly seems worth it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Fast Replacement (0.01 seconds)
by davido (Cardinal) on Jun 14, 2013 at 15:49 UTC | |
by BrowserUk (Patriarch) on Jun 14, 2013 at 19:42 UTC | |
by davido (Cardinal) on Jun 14, 2013 at 21:16 UTC | |
by BrowserUk (Patriarch) on Jun 14, 2013 at 22:13 UTC | |
by davido (Cardinal) on Jun 14, 2013 at 22:21 UTC | |
by davido (Cardinal) on Jun 16, 2013 at 16:52 UTC | |
| |
by BrowserUk (Patriarch) on Jun 14, 2013 at 21:52 UTC | |
Re^4: Fast Replacement (0.01 seconds)
by muba (Priest) on Jun 14, 2013 at 12:19 UTC |