gmpy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zakad
    New Member
    • Aug 2008
    • 5

    gmpy

    python 2.5.2
    ubuntu 8.04 LTS

    (1) I find that python/gmpy is about 3 times slower than c++/gmp.
    Is this the general experience, or am I doing something wrong?

    (2) I am unable to understand the following error in conversion from float() to gmpy.mpf() or how to avoid it:

    Code:
    import gmpy
    gmpy.set_minprec(100)
    t= gmpy.mpf(7.99)
    print t
    print t/3
    prints:
    7.9900000000000 002131628207280 30055761337
    2.6633333333333 334043876069093 43351920446

    I am new to both linux and python and would appreciate any expert help.

    zakad
  • casevh
    New Member
    • Oct 2008
    • 2

    #2
    Hi,

    Can you provide an example for performance question?

    Python will have more overhead for each function call, so it will be slower when making multiple calls. The amount of slowdown should decrease as the size of the numbers increases.

    Regarding your conversion question, you are converting the floating point approximation to 7.99 which is only accurate to about 18 decimal digits. If you convert the string "7.99" to an mpf, you'll get the value that I think you are expecting.

    casevh

    Comment

    • zakad
      New Member
      • Aug 2008
      • 5

      #3
      I found the following draft in my computer, but apparently, for some reason, it has never been posted. So, with apologies to casevh, here it is:

      Thank you, casevh. Your reply certainly answered my question as to the conversion from float (or double) to mpf . I failed to find the answer through googling.
      With regard to my first question re/ the speed of Python relative to C++, the program that I tested is based on binary splitting which, as you know, entails recursive calls to a function from within the function itself. All operations are on integers (the largest being 9092390 decimal digits long). Then, there is a single final floating point division. The results were : python/gmpy 81.638, C++/GMP 28.800 sec.
      Your comment that Python has more overhead for each function call may partly explain the relative slowness of Python. I have since come across articles that suggest that Python is significantly slower in performing simple arithmetic operations. See, for example:

      Lastly, there is a clear difference between Python and C++. Whereas in C++ the type of each operand is strictly defined at the time it is declared, Python mostly leaves the definition to the "context" during run-time. This may cause slowness and is a potential source of confusion to programmer and program alike. It is a pity because I have come to enjoy Python as a language of simple elegance as well as power and accuracy.

      Comment

      • casevh
        New Member
        • Oct 2008
        • 2

        #4
        Hello, if you are still interested in trying to resolve the performance differences, I have a question/suggestion.

        Is it always approximately 3x slower, or does it get worse when the numbers are very large? The current beta version is more efficient with memory handling and that may make a difference.

        If that doesn't make a difference, can you post the code?

        casevh

        Comment

        Working...