"J. Gleixner" <glex_n...@qwest-spam-no.invalid> writes:
[...]
> You might be tempted to use "$#array + 1" to tell you how many
> items there are in an array. Don't bother. As it happens,
> using @array where Perl expects to find a scalar value
> ("in scalar context") will give you the number of elements
> in the array:
>
> if (@animals < 5) { ... }
This is also a stupid thing to do because evaluating @animals in
scalar context means perl will do the + 1 in compiled code, as opposed
to execuing Perl (perl?) bytecode to perform the same operation.
> Seriously though, if your code is slow, it's not because you are
> using $# or scalar @arr. Either one is not going to be *that*
> expensvce' [expensive].
... and if you just insert a
for my $i (1 .. 100) {
my $j = $i * 938289897227;
}
between any two lines of the 'productive' code, that's probably also
not going to have much of a detrimental effect. But that's not a good
reason to actually do that.
> "premature optimization is the root of all evil." -- Donald Knuth
In 1943, Joseph Goebbels held an infamous speech in the so-called
'Sportpalast' in Berlin where he repeatedly asked his (carefully
selected) audience whether they wanted to have 'the total war' and
elicited enthusiatic 'Yes! Yes!' shoutings from the audience. Since
this has been recorded on film, the affirmative part of this event
really ought to be useful as generic answer to any question aka
'everything can be proven with the help of a set of suitable
out-of-context quotes'.
In the given case, this 'quote' used to be a piece of advice to people
writing machine code supposed to be executed in the fairly byzantine
(by today's standards) CISC CPUs Knuth happened to be intimately
familiar with: "Don't worry about making maximally clever use of the
instruction set of the machine. Rather build a working 'inefficient'
solution and improve that afterwards". Translated to the 'Perl
universe', it should roughly become "Don't use the 'Perl golf'
approach for solving actual problems". Rather different from the use
you put it to, isn't it?