in reply to Re^2: porting C code to Perl -- solved
in thread porting C code to Perl

I spotted the type casting of int but I have not thought it constrained all the operations too

In C, when you say int q;, that variable is fixed to, depending on how your compiler defines it, a 32 (or 16 or 64) bit signed integer value, no more, no less - simplifying a bit, no matter what you try to assign to it, it will be cast back to an int.

Now I ported to a more perlish versions, but speaking frankly, I'm quite struggling with it's efficiency.

Since functionally the two programs are practically identical, that'll simply be the difference between a complied and interpreted language. I don't have much time for optimization right now, but you might try to compare the performance of the program with and without use integer; - I'm not sure if it'll make a significant difference but it's worth a try.

In reality I'm working blindly not have well understood wht the code do.

A bit of googling on the algorithm brings me to this page, while it's a bit difficult to read visually, it seems to give an example-based explanation of the "spigot algorithm" that this appears to be an implementation of.

Replies are listed 'Best First'.
Re^4: porting C code to Perl -- use integer bench
by Discipulus (Canon) on Oct 24, 2017 at 07:12 UTC
    Hello again,

    you guessed rigth! use integer; boosts performance (if I have build my benchmarks correctly..)

    Benchmark: running no_integer_module , with_integer_module for at lea +st 20 CPU seconds... no_integer_module : 21 wallclock secs (20.64 usr + 0.00 sys = 20.64 +CPU) @ 0.92/s (n=19) with_integer_module: 21 wallclock secs (20.50 usr + 0.00 sys = 20.50 +CPU) @ 1.37/s (n=28) s/iter no_integer_module with_integer_module no_integer_module 1.09 -- -33% with_integer_module 0.732 48% --

    I read in the docs of the integer module:

    > On many machines, this doesn't matter a great deal for most computations, but on those without floating point hardware, it can make a big difference in performance.

    The above results show that my machine has not floating point hardware?

    Thanks also for the link to the spigot explication; I must admit that yesterday night I've understood almost nothing.. I'll try under sun beans.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Your benchmark looks ok, thanks, although the difference is a little smaller on my machine (-21% / +27%). I did surround your three divisions with int() in the no_integer version.

      The above results show that my machine has not floating point hardware?

      I doubt it, rather I guess that Perl can simply use more efficient implementations of the mathematical opcodes, that don't need to worry about all the floating-point stuff that Perl hides in its numerical types.