mibro8 has asked for the wisdom of the Perl Monks concerning the following question:

I'm using one function and trying to figure out how to get more performance out of this module. I figure everything is loaded in ram (perl/Math::CDF/DCDFLIB) after the 'use Math::CDF' statement.

1. Does anybody know how much performance cost their is with perl contacting a c library versus running the logic directly in perl?

2. Are there any tricks to getting better performance in a general case where you are calling the same function many times?

Thanks,
Michael

Replies are listed 'Best First'.
Re: Math::CDF performance
by kyle (Abbot) on Aug 13, 2008 at 21:57 UTC

    One thing you might try before targeting anything for optimization is Profiling your code. Profiling will tell you where your time is really spent, so you don't have to guess.

    If you're calling an expensive function many times, you might look at Memoize. This assumes that some input always produces the same output, and it uses memory to gain speed.

Re: Math::CDF performance
by BrowserUk (Patriarch) on Aug 13, 2008 at 23:28 UTC

    Looking at the XS, there seems to be very little code between your perl and the C library. About the only way you might speed that up is to write some more XS that took an array of sets of parameters and called the underlying C function in a tight C-loop accumulating the results before passing them back to Perl.

    But for that to be beneficial you would have to be calling the same function many (many) times with different parameters sets. And the logic of your program would have to lend itself to it. Eg. You would have to know the parameters for many calls at the same time (as opposed to subsequent sets of parameters being derived from previous results sets).

    Which function are you calling and how many times that this is the bottleneck?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Math::CDF performance
by mibro8 (Initiate) on Aug 14, 2008 at 00:14 UTC
    Thanks so much for the help thus far, turns out based on a dprofpp -u of the program it is probably not Math::CDF at all but the Math::BigInt / Math::BigFloat

    %Time ExclSec CumulS #Calls sec/call Csec/c  Name
     17.0   1.665  1.679  13251   0.0001 0.0001  Math::BigInt::Calc::_div_use_div
     13.4   1.306  1.306 136012   0.0000 0.0000  Math::BigInt::Calc::_zeros
     12.4   1.213  1.214  45800   0.0000 0.0000  Math::BigInt::Calc::_rsft
     12.3   1.202  1.225  28036   0.0000 0.0000  Math::BigInt::Calc::_mul_use_div
     12.2   1.189  1.189  87581   0.0000 0.0000  Math::BigInt::Calc::_str
     7.83   0.763  3.727 134446   0.0000 0.0000  Math::BigFloat::bnorm
     7.24   0.705  0.705 156384   0.0000 0.0000  Math::BigInt::Calc::_new
     7.22   0.703  0.720  22286   0.0000 0.0000  Math::BigInt::Calc::_lsft
     6.21   0.605  6.006  50616   0.0000 0.0001  Math::BigInt::round
     6.00   0.584  1.885  41401   0.0000 0.0000  Math::BigInt::bround
     5.07   0.494  0.741  41894   0.0000 0.0000  Math::BigInt::new
     4.23   0.412  0.412  98690   0.0000 0.0000  Math::BigInt::accuracy
     3.93   0.383  0.481 189104   0.0000 0.0000  Math::BigFloat::is_zero
     3.38   0.329  2.803  16346   0.0000 0.0002  Math::BigFloat::badd
     3.13   0.305  0.305  63091   0.0000 0.0000  Math::BigInt::Calc::_add
    

    If I remove the "use bignum;" one of the numbers I calcualte is abnormally large and retuns a zero value unless I turn on bugnum.......