[geeks] Programming question.

David Cantrell david at cantrell.org.uk
Tue Apr 2 09:51:08 CST 2002


On Tue, Apr 02, 2002 at 10:09:50AM -0500, alex j avriette wrote:
> >> In perl ...
> >>
> >> use Quantum::Superpositions;
> >> @C = eigenstates(any(@B) != all(@A));
> sick. sick. sick.

Why, thankyou! :-)

> my earlier example is most likely the best way to do this (barring some 
> XS backend module which specializes in it -- list::util does not). the 
> above solution may well work (i havent tested it), but youre using at 
> least twice the memory, and moving a LOT slower. in fact, map is usually 
> 1-30% slower than foreach in every instance (and it has been said that 
> using for instead of map is only for syntactic wussies who are afraid 
> of $_).

You will notice that I use map once and foreach once - that is, iterate
through each list ONCE.  Your solution iterates through one of the lists
once, and the other list once for each of those iterations.  It also
allocates and de-alocates memory each time round the outer loop.  Yuck.

> when youre iterating over an array, use map, grep, and sort. thats what 
> theyre there for. they have the unfortunate consequence of using more 
> memory (ive consumed > 3gb before with a 15-layer map over 350,000 rows 
> from a database, but thats extreme), but it is *tremendously* quicker 
> because of the way it is handled in the back end (there are certain 
> assumptions made for a map that arent made for a for).

It is quicker UNLESS you nest them inside each other.  yes, nesting your
list operations inside each other gives you a nice warm feeling inside
because it makes your code shorter and makes it look more like lisp, but
it sucks.

> anyhow, one more admonition about perl. *its not c!* program in perl 
> when youre writing code in perl.

Your grandmother is that way ---> and needs a lesson in sucking eggs.

> oh yeah. and print "@array" does what you want. look into the special $" 
> variable.

Use of special variables in demo code for an audience not skilled in the
black arts of perl-fu considered silly.

-- 
Lord Protector David Cantrell     |     http://www.cantrell.org.uk/david

   I hear you asking yourselves "why?".  Hurd will be out in a
   year ...
        -- Linus Torvalds, in <1991Oct5.054106.4647 at klaava.Helsinki.FI>



More information about the geeks mailing list