-4

Hello I wrote some code in Python, using the mpmath, arbitrary precision math module:

from __future__ import division
from numpy import arctan, sin, absolute, log10
from mpmath import *
import time
imax = 1000001
x = mpf(0)
y = mpf(0)
z = mpf(0)
t = mpf(0)
u = mpf(0)
i = 1
mp.prec = 128
start_time = time.time()
while i < 1000001:
    i += 1
    x = mpf(x + 1)
    y = mpf(y + x * x)
    z = mpf(z + sin(y))
    t = mpf(t + absolute(z))
    u = mpf(u + log10(t))

print("--- %s seconds ---" % (time.time() - start_time))
print x
print y
print z
print t
print u

It took approximately 87 secs to print results. How can I improve this code? My friend in Fortran wrote similar program and it took only 3.14 secs for him to print results.

hpaulj
  • 175,871
  • 13
  • 170
  • 282
  • 1
    Hi. There's a SE website for code review which is more appropriate for this type of question: https://codereview.stackexchange.com – dietbacon Nov 22 '15 at 18:19
  • Fortran is about as fast as Pi – rolfl Nov 22 '15 at 18:21
  • 1
    Also the question is very poorly written. You should at least explain what your goal with the code is instead of having us guessing. – dietbacon Nov 22 '15 at 18:23
  • 3
    @dietbacon and OP, this would not be well-received on Code Review, as there is nothing in the title and question that gives any kind of explanation what this code is doing. Also, just like on Stack Overflow, " is this language better than this other language?" would of course be very opinion-based. – Phrancis Nov 22 '15 at 18:38
  • 1
    This has been cross-posted to http://codereview.stackexchange.com/questions/111516/improving-speed-of-my-code – Phrancis Nov 22 '15 at 18:39
  • 1
    It seems as if you've managed to create duplicate accounts, http://stackoverflow.com/users/4389410/iwko and http://stackoverflow.com/users/5263520/iwko-fronc – Vatine Nov 22 '15 at 19:05
  • I've edited the subject line and text to highlight the use of `mpmath`. – hpaulj Nov 23 '15 at 01:52

1 Answers1

1

Something which got lost in the comments is this code uses the mpmath module. The * import, obscures the fact all the calculations use special functions from this module:

from mpmath import *
mpf(x + 1)

mpmath is a free (BSD licensed) Python library for real and complex floating-point arithmetic with arbitrary precision

All the impressions that numpy users have about speed, and the ways of speeding numpy code, are irrelevant. There are 2.9K followers of the numpy tag, 0 followers of the mpmath tag. The CodeReview fit is even worse.

As for the Fortran comparison, you don't even indicate what mp math package your friend is using. Compiled Fortran code will alway be faster than Python code, though good use of numpy 'vectorization' can reduce the speed difference. But this code example does not try to use any of those numpy tricks (even if they are possible with mpmath). It is just doing calculations on single numbers, not arrays.

There is a google groups list for mpmath questions. I'd suggest using that.

=========================

So someone familiar with extended precision calculations has responded to your CR question.

hpaulj
  • 175,871
  • 13
  • 170
  • 282