if we're talking about optimization and the question was really how to CAPTURE the characters after the last / there is really NO need for a s/// and a simple m// would do which i imagine will also be quite a bit faster
regards, jos ----- Original Message ----- From: "Gary Hawkins" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]>; "David" <[EMAIL PROTECTED]> Sent: Sunday, January 06, 2002 11:48 PM Subject: RegEx speed (was: Using regexp to get a substring) > That'll work, but on a finer point, if you need to be thinking about > optimization at the moment: > > ($substring1 = $string) =~ s/.*\\(.*)/$1/; > > ....is about 4 times slower than: > > ($substring2 = $string) =~ s/.*\\//; > > ....because the second one doesn't have to store the matched value inside the > parens in $1. > > In test below: > substring1 code took: 20 wallclock secs (20.05 usr + 0.00 sys = 20.05 CPU) > substring2 code took: 5 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) > > So it can be best to use $1 thru $9 only when necessary if speed matters. > > Gary > > ====== > > Notes: > > In this test script we do the substitution a million times for each method and > then print the benchmark results. You can tailor this script to check the > comparison on any two or more different ways of doing something. > > #!perl.exe > > use Benchmark; > > $string ='C:\PerlScripts\TestSamples\StringTest.pl'; > > # substring1 method > $i = 0; > $t0 = new Benchmark; > while ($i < 1000000) { > ($substring1 = $string) =~ s/.*\\(.*)/$1/; > $i++; > } > $t1 = new Benchmark; > $td = timediff($t1, $t0); > print "substring1 code took: ", timestr($td), "\n\n"; > > # substring2 method > $i = 0; > $t0 = new Benchmark; > while ($i < 1000000) { > ($substring2 = $string) =~ s/.*\\//; > $i++; > } > $t1 = new Benchmark; > $td = timediff($t1, $t0); > print "substring2 code took: ", timestr($td), "\n\n"; > > __END__ > > Printed result: > > substring1 code took: 20 wallclock secs (20.05 usr + 0.00 sys = 20.05 CPU) > > substring2 code took: 5 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) > > > > -----Original Message----- > > From: Shawn [mailto:[EMAIL PROTECTED]] > > Sent: Sunday, January 06, 2002 1:03 PM > > To: [EMAIL PROTECTED]; David > > Subject: Re: Using regexp to get a substring > > > > > > "David" <[EMAIL PROTECTED]> wrote in message > > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > > Hello All, > > > > > > I am beginner and need some helps. Thanks a lot! > > > > > > The question is, if I have a string, for example > > > "C:\PerlScripts\TestSamples\StringTest.pl", how do I use regexp to parse > > > this string and get substring after the last backslash ("StringTest.pl"). > > > > Hey David, > > This should work for you: > > > > use strict; > > my $String='C:\PerlScripts\TestSamples\StringTest.pl'; > > (my $substring =$string) =~s/.*\\(.*)/$1/; > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]