[33019] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4295 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 17 09:09:19 2014

Date: Fri, 17 Oct 2014 06:09:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 17 Oct 2014     Volume: 11 Number: 4295

Today's topics:
        fast math? (Was: prime) <bauhaus@futureapps.invalid>
    Re: fast math? (Was: prime) <gamo@telecable.es>
    Re: fast math? (Was: prime) <hjp-usenet3@hjp.at>
    Re: fast math? (Was: prime) <gamo@telecable.es>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 16 Oct 2014 21:28:43 +0200
From: "G.B." <bauhaus@futureapps.invalid>
Subject: fast math? (Was: prime)
Message-Id: <m1p698$l00$1@dont-email.me>

On 16.10.14 19:52, gamo wrote:
> View the numbers, there is no need to long long types, a unsigned long
> (typically 64 bit number) is faster and could do the job.

Yes, native int, compiled on a 64 bit platform, easily outperforms
a equivalent program written in Perl. More reason for me to ask:
Is there a package in use with Perl that would speed up int
computations in Perl?  Math::GMP does not seem to be it (not too
surprising). I imagine a test program like this one, in C and Perl:

# -8<-

/* pointless */
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>

typedef uint64_t num;

num f(const unsigned short divisor, const num init, const num stop)
{
   num result = init;

   for (num m = divisor; m > 0; ++m) {
     while (1) {
       result += m % 2 ? (2*m) : m;
       result /= divisor;
       if (result >= stop) {
         m = -1;
         break;
       }
       result *= divisor;
     }
   }
   return result;
}

int main()
{
   for (unsigned short runs = 3; runs; --runs) {
     printf("result=%" PRIu64 "\n", f(300, 1000, 1<<28));
   }
   return 0;
}

# -8<-

# pointless
use integer;

sub f
{
     my ($divisor, $init, $stop) = @_;
     my $result = $init;

     for (my $m = $divisor; $m > 0; ++$m) {
         while (1) {
             $result += $m % 2 ? (2*$m) : $m;
             $result /= $divisor;
             if ($result >= $stop) {
                 $m = -1;
                 last;
             }
             $result *= $divisor;
         }
     }
     return $result;
}

for (my $runs = 3; $runs; --$runs) {
     printf("result=%lu\n", f(300, 1000, 2**28));
}




------------------------------

Date: Thu, 16 Oct 2014 21:55:09 +0200
From: gamo <gamo@telecable.es>
Subject: Re: fast math? (Was: prime)
Message-Id: <m1p7qt$hlf$1@speranza.aioe.org>

El 16/10/14 a las 21:28, G.B. escribió:
> Yes, native int, compiled on a 64 bit platform, easily outperforms
> a equivalent program written in Perl. More reason for me to ask:
> Is there a package in use with Perl that would speed up int
> computations in Perl?  Math::GMP does not seem to be it (not too
> surprising). I imagine a test program like this one, in C and Perl:

Clearly the response is no. You can't speed up native types.
A module for the various incarnations of GMP in Perl exists
under the directory /demos of GMP library, but this only
applies to big numbers, which are of ocassional use. And for
big numbers I understand numbers ahead of 2**49, wich is
big enough to store common currency numbers (e.g. national
accounting numbers). To store US debt numbers you need GMP ;-)

-- 
http://www.telecable.es/personales/gamo/


------------------------------

Date: Fri, 17 Oct 2014 13:41:17 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: fast math? (Was: prime)
Message-Id: <slrnm4202t.j7p.hjp-usenet3@hrunkner.hjp.at>

On 2014-10-16 19:55, gamo <gamo@telecable.es> wrote:
> El 16/10/14 a las 21:28, G.B. escribió:
>> Yes, native int, compiled on a 64 bit platform, easily outperforms
>> a equivalent program written in Perl. More reason for me to ask:
>> Is there a package in use with Perl that would speed up int
>> computations in Perl?

Inline::C ;-)

>> Math::GMP does not seem to be it (not too
>> surprising). I imagine a test program like this one, in C and Perl:
>
> Clearly the response is no. You can't speed up native types.

I agree with the conclusion, but not the reason. 

The "native type" in Perl (there really is only one relevant here) is a
rather complex thing (a scalar can be a signed int, an unsigned int, a
floating point number, a string, or several of these things at once)
and if you look at the perl source code you will probably be shocked to
see how many lines of C code even simple operations need. 

So if a module could replace say "add" with a specialized routine which
really just adds two ints, that would be faster than perl's "add two
arbitrary scalars" op. 

But I think that this wouldn't improve overall performance much.  You
still have all the interpreter overhead, you still have to provide valid
scalars (with reference counters and stuff), etc.

Of course, if you care about computational performance, you often don't
deal with individual scalars, but with arrays (vectors, matrices, ...)
and then PDL might be your friend.

        hp


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


------------------------------

Date: Fri, 17 Oct 2014 14:48:16 +0200
From: gamo <gamo@telecable.es>
Subject: Re: fast math? (Was: prime)
Message-Id: <m1r36j$sqk$1@speranza.aioe.org>

El 17/10/14 a las 13:41, Peter J. Holzer escribió:
>>> a equivalent program written in Perl. More reason for me to ask:
>>> >>Is there a package in use with Perl that would speed up int
>>> >>computations in Perl?
> Inline::C;-)
>

I try Inline::C

no better performance at all for alternatives PRNGs

-- 
http://www.telecable.es/personales/gamo/


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4295
***************************************


home help back first fref pref prev next nref lref last post