Hi Pierre,

On 2016-12-03, Pierre <pierre.guil...@gmail.com> wrote:
> We are talking about very basic advice to give to a beginner, and so, I 
> knew enough to say that well, C ints (so I guess numpy.int's) will be fast, 
> but limited in size, and elements of ZZ can be as large as your memory 
> allows etc (and a similar, but different, discussion with RDF (or 
> numpy.float) and RR etc). However, I am unable to write a piece of code 
> that would clearly show the difference in speed ! 

I'd put the test loop into a Cythonized function (in order to reduce
the overhead of Python loops):

sage: cython("""
....: def testfunc(a,b,o):
....:     cdef int i
....:     for i in range(1000000):
....:         x = o(a,b)
....: """)
sage: %timeit testfunc(1234123,1234123, operator.add)
10 loops, best of 3: 58.7 ms per loop
sage: %timeit testfunc(1234123,1234123, operator.mul)
10 loops, best of 3: 60.7 ms per loop
sage: %timeit testfunc(int(1234123),int(1234123), operator.add)
10 loops, best of 3: 49.5 ms per loop
sage: %timeit testfunc(int(1234123),int(1234123), operator.mul)
10 loops, best of 3: 48.9 ms per loop
sage: %timeit testfunc(RR(1234123),RR(1234123), operator.add)
1 loop, best of 3: 233 ms per loop
sage: %timeit testfunc(RR(1234123),RR(1234123), operator.mul)
1 loop, best of 3: 244 ms per loop
sage: %timeit testfunc(QQ(1234123),QQ(1234123), operator.add)
1 loop, best of 3: 430 ms per loop
sage: %timeit testfunc(QQ(1234123),QQ(1234123), operator.mul)
1 loop, best of 3: 686 ms per loop

and for exponentiation I'd use a shorter loop:

sage: cython("""
....: def testfunc(a,b,o):
....:     cdef int i
....:     for i in range(100):
....:         x = o(a,b)
....: """)
sage: %time testfunc(1234123,1234123, operator.pow)
CPU times: user 18.2 s, sys: 0 ns, total: 18.2 s
Wall time: 18.2 s

and very surprisingly (to me at least) exponentiation takes a
lot longer when using int(1234123)**int(1234123). Should a Python int
not simply give a wrong result but at least be a lot faster than an
arbitrary precision integer??

Anyway, with shorter numbers, I get

sage: %timeit testfunc(1234,123, operator.pow)
10000 loops, best of 3: 44.8 µs per loop
sage: %timeit testfunc(int(1234),int(123), operator.pow)
1000 loops, best of 3: 212 µs per loop
sage: int(1234)**int(123)==1234**123
True
sage: type(int(1234)**int(123))
<type 'long'>

Cheers,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to