What application could you have that needs a precise value of this product? I would be very surprised if you could not get the accuracy you require using the Stirling_approximation. In any case, if your question is optimization, then you need to test. Specifically, you need to use benchmarking (see Benchmark) and profiling (see Devel::NYTProf) to determine where exactly your bottlenecks are.

For this operation, one very simple optimization would be swapping from a for to a while loop since Foreach Loops need to construct the list before beginning iteration. threads is also a fairly simple way to improve performance. Here is a simple benchmark, showing a 38% speedup using a while loop and two threads. Note the speedup is not higher because creating threads involves overhead.

Threading in this case does not make sense - the overhead in starting a thread exceeds the performance boost. For loops with range operators are automatically transformed into efficient C-style loops internally by Perl. But you should really be using the Stirling_approximation and only storing the log of your value to avoid overflow.

#!/usr/bin/perl use strict; use warnings; use Benchmark qw':all :hireswallclock'; use threads; cmpthese(10, { 'while' => sub { my $i = 230000; my $k = 1; while ($i <= 900000) { $k *= $i++; } }, 'for' => sub { my $k = 1; for (230000 .. 900000) { $k *= $_; } }, 'threads' => sub { my $k = 1; my $thr2 = async { my $k = 1; my $i = 565001; while ($i <= 900000) { $k *= $i++; } }; my $i = 230000; while ($i <= 565000) { $k *= $i++; } $k *= $thr2->join(); } });

outputs

Rate threads while for threads 3.09/s -- -29% -38% while 4.33/s 40% -- -13% for 4.96/s 60% 15% --

And if you look at the results from these multiplications, you have likely exceeded the maximum value representable by floating point on your machine.

Update: Need caffeine, read my Benchmark results backwards, fixed above.


In reply to Re: how to multiply an array of number as fast as posible by kennethk
in thread how to multiply an array of number as fast as posible by baxy77bax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.