Hi again,
I hope you don't mind me bumping this thread one more time. I started
experimenting with trying a few things for fast arbitrary precision
computations using Cython. Above it was suggested to use MPFR
directly, so without the RealNumber wrapper, as the fastest way. Here
is a bit of code that does the same thing (computing x**2+i*x for
input x and i in range(10**7)) using RealNumber, using doubles and
using MPFR directly:
def run2(RealNumber x):
"""Using RealNUmber:"""
cdef RealNumber res=x._new()
cdef RealNumber tmp=x._new()
cdef int i
for i in range(10**7):
mpfr_mul_ui(tmp.value,x.value,i,GMP_RNDN)
mpfr_mul(res.value, x.value, x.value, GMP_RNDN)
mpfr_add(res.value,res.value,tmp.value,GMP_RNDN)
return res
def run3(double x):
"""Using doubles:"""
cdef double res
cdef int i
for i in range(10**7):
res=x**2+x*i
return res
def run4(double x):
"""Using MPFR directly:"""
cdef mpfr_t xx, res, tmp
cdef int i
mpfr_set_default_prec(53)
mpfr_init(xx); mpfr_init(res); mpfr_init(tmp)
mpfr_set_d(xx,x,GMP_RNDN)
for i in range(10**7):
mpfr_mul_ui(tmp,xx,i,GMP_RNDN)
mpfr_mul(res, xx, xx, GMP_RNDN)
mpfr_add(res,res,tmp,GMP_RNDN)
return mpfr_get_d(res,GMP_RNDN)
and using the following bit of code in Sage:
RRR=RealField(53)
xx=random()
y=RRR(xx)
print "Using RealNumber:"
time res=run2(y)
print res
print "Using doubles:"
time res=run3(float(xx))
print res
print "Using MPFR directly:"
time res=run4(float(xx))
print res
I get the following output:
Using RealNumber:
Time: CPU 2.41 s, Wall: 2.51 s
4.54560477019471e6
Using doubles:
Time: CPU 0.02 s, Wall: 0.02 s
4545604.77019
Using MPFR directly:
Time: CPU 2.38 s, Wall: 2.87 s
4545604.77019
What surprises/disappoints me a bit is that both RealNumber and MPFR
directly are a factor 100 slower than using doubles, even though I'm
using RealField(53). Is this something I just have to live with, i.e.
computations with doubles are somehow just more optimized (?), or did
I do something wrong/is there something I can do to improve the speed
of the RealNumber/MPFR directly variations?
Many thanks, Kees
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org