Connecting Tech Pros Worldwide Help | Site Map

gmpy

  #1  
Old September 29th, 2008, 06:02 PM
Newbie
 
Join Date: Aug 2008
Posts: 5
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:

Expand|Select|Wrap|Line Numbers
  1. import gmpy
  2. gmpy.set_minprec(100)
  3. t= gmpy.mpf(7.99)
  4. print t
  5. print t/3
  6.  
prints:
7.990000000000000213162820728030055761337
2.663333333333333404387606909343351920446

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

zakad
  #2  
Old October 3rd, 2008, 07:05 PM
Newbie
 
Join Date: Oct 2008
Posts: 2

re: gmpy


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
  #3  
Old August 17th, 2009, 06:04 PM
Newbie
 
Join Date: Aug 2008
Posts: 5

re: gmpy


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:
http://furryland.org/~mikec/bench/
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.
  #4  
Old September 12th, 2009, 12:26 AM
Newbie
 
Join Date: Oct 2008
Posts: 2

re: gmpy


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
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
module confict? gmpy and operator mensanator@aol.com answers 3 May 25th, 2006 02:25 AM
gmpy 1.01 rc near... anybody wanna test> Alex Martelli answers 22 November 22nd, 2005 12:35 AM
GMPY: divm() memory leak revisited mensanator@aol.com answers 3 November 6th, 2005 06:15 PM
gmpy 1.0 for python 2.4 alpha 2 Windows-packaged Alex Martelli answers 11 July 18th, 2005 04:32 PM
Gmpy Jean-Pierre Andreaux answers 8 July 18th, 2005 02:23 AM