perl and math - Page 2

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: perl and math need help with printf or other

[while loops]
Quoted text here. Click to load it

No, on the contrary. He writes that in some cases you may need to use a
while loop instead of a foreach loop for performance reasons (I think
this is only a concern if the list has to generated at the time of the
loop), but otherwise a foreach loop loop is preferable for stylistic

I agree with that.


   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   |         | die Satzbestandteile des Satzes nicht mehr
__/   | | zusammenpaƟt. -- Ralph Babel

Re: perl and math need help with printf or other

Quoted text here. Click to load it

'A list' is a somewhat hazily defined term. Depending on where the
values-to-be-iterated-over actually come from, a foreach loop may well
be faster than a more C-style one because more of the processing is done
by native code which is part of perl, example

use Benchmark;

my @a = 1 .. 10000;

       direct => sub {
           my $s;
           for (@a) {
           $s += $_;
       indirect => sub {
           my ($i, $s);

           for ($i = 0; $i < @a; ++$i) {
           $s += $a[i];

But the situation is somewhat different for printing a large string
part-by-part. A loop based on substr will work with one substring at a
time so the amount of additional memory needed for performing the algorithm does
not depend on the size of the string. OTOH,

print for /.{1, 70}/g

will create a list of all substrings of the input string on the stack
prior to looping (the same is true for generating any kind of list, as
opposed to 'iterating over an existing one') so this will need more than
twice as much memory as the substr-based variant for processing the same
input. But unless it is known that the inputs may be seriously large,
this should IMHO be ignored.

Re: perl and math need help with printf or other

El 21/09/14 a las 05:51, Martin Eastburn escribiĆ³:
Quoted text here. Click to load it

If you want to print and store the results as a form of sign of
intelligent life, I recommend you that ask for 10002 digits of
pi, and after that, chop the last 2 digits just to be sure there
are not rounding problems due to the algorithm. It could happen.


Site Timeline