Re: looping 2 times through 5000 differs from 5000 times through 2

[email protected] ("Jenda Krynicky")
Newsgroups perl.beginners
Message-ID <[email protected]>
From: Rob Dixon <[email protected]>
> oldgeezer wrote:
> > foreach my $entry (uniq @IK){
> >   foreach my $line(@DATA) {
> >     # Note /;$entry;/ takes longer than
> >     # /$entry;/ and that takes longer than /$entry/
> >     # But /$entry/ also splits remarks_entries.
> >     # So I need the trailing ';' because they
> >     # are not in the remarks_entries
> >     # Appending the i, like: /$entry/i slows
> >     # down too. I don't need it.
> >     # Prepending m, like m/$entry/
> >     # does not change anything (timewise).
> >     # For the average person /$entry;/ is fastest
> >     if($line=~ /$entry;/) {
> >      splitline($line); #@IK changes when I splitline
> >      #next; don't do this. It makes it slower
> >     };
> >   };
> > };
> 
> Your comments are fine, but they have obscured your code structure. How about
> 
> # Note /;$entry;/ takes longer than /$entry;/ and that takes
> # longer than /$entry/ But /$entry/ also splits
> # remarks_entries. So I need the trailing ';' because they
> # are not in the remarks_entries.
> #
> # Appending the i, like: /$entry/i slows down too. I don't
> # need it. Prepending m, like m/$entry/ does not change
> # anything (timewise). For the average person /$entry;/ is
> # fastest
> #
> foreach my $entry (uniq @IK) {
> 
>   foreach my $line (@DATA) {
> 
>     if ($line=~ /$entry;/) {
> 
>       splitline($line);  # @IK changes when I splitline
>       #next; don't do this. It makes it slower
>     }
>   }
> }
> 
> But even then I have issues with what your comments say. First of all you are
> obsessed with speed over function. A programmer's primary duty is to make things
> that work, and look like they work; it is only a secondary consideration to make
> things faster if they turn out to be too slow.

Agreed. OTOH, in this case one might actually make it even quicker 
using qr//:

 foreach my $entry (uniq @IK) {
   my $entry_re = qr/$entry;/; 
   foreach my $line (@DATA) {
 
     if ($line=~ $entry_re) {
 
       splitline($line);  # @IK changes when I splitline
       #next; don't do this. It makes it slower
     }
   }
 }

Another thing ... if splitline($line) indeed does change @IK (which 
is a bad thing, it should not modify something behind the scenes) do 
you expect the outer foreach to note that? I hope not, in any case, 
it doesn't.

Especially if the $entry is long and is actually a regexp and not a 
string of letters. Of course if it's not meant to be a regexp you 
should use
   my $entry_re = qr/\Q$entry\E;/; 

Jenda
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.