in reply to japhy blabs about regexes (again)

Being curious as Perl people will be, i tired an rindex based solution. It unfortunatly would only work with true spaces (" "), and did not fare too well in testing, but, we all learn when things like this happen ... here is my code/output :

Code Addition :
P_rindex => sub { my $x = $X; while (1) { my $i = length($x); (rindex($x," ") == $i ? chop : las +t) }; }, F_rindex => sub { my $x = $Y; while (1) { my $i = length($x); (rindex($x," ") == $i ? chop : las +t) }; },


Output :
Benchmark: running F_plus, F_rindex, F_sexeger, F_while, P_plus, P_rin +dex,P_sexeger, P_while, each for at least 5 CPU seconds... F_plus: 6 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) @ 37 +600.97/s(n=193645) F_rindex: 6 wallclock secs ( 5.45 usr + 0.00 sys = 5.45 CPU) @ 59 +183.30/s(n=322549) F_sexeger: 7 wallclock secs ( 5.24 usr + 0.00 sys = 5.24 CPU) @ 59 +187.60/s(n=310143) F_while: 7 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 69 +741.02/s(n=368930) P_plus: 5 wallclock secs ( 5.46 usr + 0.00 sys = 5.46 CPU) @ 33 +136.08/s(n=180923) P_rindex: 6 wallclock secs ( 5.31 usr + 0.00 sys = 5.31 CPU) @ 60 +743.69/s(n=322549) P_sexeger: 5 wallclock secs ( 5.36 usr + 0.00 sys = 5.36 CPU) @ 49 +557.65/s(n=265629) P_while: 6 wallclock secs ( 5.09 usr + 0.00 sys = 5.09 CPU) @ 50 +179.17/s(n=255412) Rate P_plus F_plus P_sexeger P_while F_rindex F_sexeger P_ +rindex F_while P_plus 33136/s -- -12% -33% -34% -44% -44% + -45%-52% F_plus 37601/s 13% -- -24% -25% -36% -36% + -38%-46% P_sexeger 49558/s 50% 32% -- -1% -16% -16% + -18%-29% P_while 50179/s 51% 33% 1% -- -15% -15% + -17%-28% F_rindex 59183/s 79% 57% 19% 18% -- -0% + -3%-15% F_sexeger 59188/s 79% 57% 19% 18% 0% -- + -3%-15% P_rindex 60744/s 83% 62% 23% 21% 3% 3% + ---13% F_while 69741/s 110% 85% 41% 39% 18% 18% + 15%--

As usual, nothing fantastic, but worth the learning experiance.
OH, a sarcasm detector, that’s really useful

Replies are listed 'Best First'.
Re: Re: japhy blabs about regexes (again)
by japhy (Canon) on Jul 18, 2001 at 01:06 UTC
    I wrote four XS functions, and one of them (used here) is called rnchrpos(STR, CHARS [, POS]). It works just like rindex(), except it searches for a character that is not one of a set of characters. It's similar to doing:
    sub rnchrpos { my ($str, $chars, $pos) = @_; $pos ||= $pos; $pos += length $str if $pos < 0; return ( substr($str, 0, $pos) =~ /[\Q$chars\E]*\z/ ? $-[0] - 1 : -1 ); }
    Except that mine is faster, since it's written in C. The results vary greatly. It comes down to knowing your string. If your string has lots of chunks of whitespace, the regular regex approach is bad. If your string has a long string of trailing whitespace, the rnchrpos() approach is bad. If there's no trailing whitespace, or very little, the while approach and the rnchrpos() are good. The sexeger approach is pretty good if the string isn't very long. If your string has a few chunks of whitespace the regular regex approach is good.

    We live in a very variable world.

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;