super geek question
Eugene Tsyrklevich
eugenet at mailcity.com
Sat Feb 5 16:07:01 CST 2000
~sdpm~
that is indeed a bug in your code
http://www.perl.com/CPAN-local/doc/manual/html/pod/perlop.html#Binding_Operators
btw, your last benchmark item is broken. you say $f[$i]=~ /(\S+$)/; that applies the regular expression to $f[$i] and then it throws away the result. to fix that you can say
$f[$i] = $1 if $f[$i] =~ /(\S+$)/;
or you can do it the perl way :)
push @f, /(\S+$)/ for @fl;
^ that should be a bit faster than your original loop since for (@fl) is faster than for ($i=0; $i<@fl;$i++) which is generally faster than using map
cheers.
--
On Sat, 5 Feb 2000 13:21:09 C. Abney wrote:
>On Sat, 5 Feb 2000, Eugene Tsyrklevich wrote:
>
>> your first "solution" can be rewritten as
>>
>> @flist = map +(split)[-1], @flist;
>>
>> your second solution can be rewritten as (notice no tilda after the equal sign)
>>
>> @flist = map /(\S+$)/, @flist;
>
>These are both great! Wow you fixed my code and eliminated my problems
>at the same time. I've been poking around in the less traveled sections
>of Perl thinking I would be rewarded:
>
>(3)[1259]$ tar tvfz ~/downloads/pbs/pbs_v2.2p7.tar.gz | ./bm-tarout.plx
>Benchmark: timing 1000 iterations of COD1, CODE, LOOP, REG1, REGX...
> COD1: 24 wallclock secs (24.00 usr + 0.01 sys = 24.01 CPU)
> CODE: 27 wallclock secs (27.39 usr + 0.00 sys = 27.39 CPU)
> LOOP: 19 wallclock secs (18.66 usr + 0.00 sys = 18.66 CPU)
> REG1: 17 wallclock secs (17.22 usr + 0.00 sys = 17.22 CPU)
> REGX: 28 wallclock secs (27.82 usr + 0.00 sys = 27.82 CPU)
>
>Now I know how badly my code sucks, but less why I should be poking around
>in the less traveled sections of perl! I applied Eugene's match to a
>standard loop and it is virtually as fast as 'map'.
>
>Well, now my problems don't exist in Eugene's code, but I'm still curious
>about the second one: turn warnings on and Perl will bitch about applying
>a regex substitution to the /scalar/ context of the array. This is just
>not true (I think) and I was hoping someone could explain whether this is
>really a bug or if my code is just really brain-damaged but Perl is doing
>a pretty good job of covering for me.
>
>Thanks again for the input and I hope someone else got as much out of this
>as me <g>
>
>====================================================================
>#! /usr/bin/perl
>use Benchmark;
>my ( @a, @b, @c, @d, @e, @f );
>@fl = <>;
>timethese( 500, {
> CODE => sub { @a = @fl; @a = map { split /\s+/; $_[$#_] } @a },
> COD1 => sub { @b = @fl; @b = map +(split)[-1], @b },
> REGX => sub { @c = @fl; @c =~ map { s/^.*\s([\S]+)$/$1/ } @c },
> REG1 => sub { @d = @fl; @d = map /(\S+$)/, @d },
> LOOP => sub { @f = @fl; for ($i=0;$i<@f;$i++) {$f[$i]=~ /(\S+$)/;} }
>});
>====================================================================
>
>CA
>--
>Einstein himself said that God doesn't roll dice. But he was wrong. And
>in fact, anyone who has played role-playing games knows that God
>probably had to roll quite a few dice to come up with a character like
>Einstein. -- Larry Wall C. Abney
>
>
MailCity. Secure Email Anywhere, Anytime!
http://www.mailcity.com
~sdpm~
The posting address is: san-diego-pm-list at hfb.pm.org
List requests should be sent to: majordomo at hfb.pm.org
If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:
unsubscribe san-diego-pm-list
If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.
More information about the San-Diego-pm
mailing list