largenumber - Python: how so fast? -


Morden Twister duration used in module random (I'm told) 2 * * 19937 - 1. As a binary number, this 1 991 is in 1 line (if I am not wrong). Python is darned very fast in Davis:

$ python -m timeit '2 ** 19937' 10000000 loops, best 3: 0.0271 per loop $ python -m timeit -s 'Result = 0' 'result = = 2 ** 19937' 100000 loops, 3: 2.09 best use per loop

I think the second version needs conversion?

And it's not just binary, it's also faster (instead of showing numbers, I show the length of decimals changed in the string):

  gt; & Gt; Import Mathematics & gt; & Gt; & Gt; N = 1000 & gt; & Gt; & Gt; S = str ((int (n * math.e)) ** (int (n * math.pi))))> gt; & Gt; & Gt; Len (s) 10787 & gt; & Gt; & Gt; N = 5000 & gt; & Gt; & Gt; S = str ((int (n * math.e)) ** (int (n * math.pi))))> gt; & Gt; & Gt; Len (s) 64921  

Time:

  Python - 'Import Mathematics' -' n = 1000 '' s = str ( (Int * (n * meth. E)) ** (Int * n * methpy)) '10 loops, best 3: 51.2 ms per loop  

The question is: How is it done?

Am I affected only? I get sight of the dragon shell which instantly produces 5000 or so many places.

Edit:

Extra time recommended by @Delke and @trpp

>

$ python -m time 'x = 2' 'x ** $ 19937 '1000 loops, use the best 3: 230 per loop $ dragon -m time' x = 2 '' int (x ** 19937) '1000 loops, best 3: 232 per loop per pipe $ Python- M time 'x = 2' 'str (x ** 19937)' 100 loops, best 3: 16.6 msec per loop $ dragon-m time - 'S' result = 0 '' x = 2 '' result + = x ** 19937 '1000 loops, best 3: 237 per loop $ python -m time-value' 'result = 0' 'x = 2' ' Result + = x ** 19937 '' int (result) '1000 loops, use the best 3: 238 per pipe $ $ python -m timeit -s' result = 0 '' x = 2 '' result + = x ** 19937 '' St (result) '100 loops, best 3: 16.6 msec per loop

So it looks like me result = 0; Results + = 2 ** 19937 Perhaps the emphasis is on conversion.

Python converts it into Davis, which is very fast.

I do not have Python, but no, there is no need to do this. There is no need of computation with 2 ^ 19937, it is a binary change with only 1993 ("<"

Edit: Exponent transfer can be similar to the point (= to move the point) if the number base exponent base.

10 ^ -1 = 0.1 10 ^ 0 = 1 = 10 ^ 1 = 10 = 10 ^ 2 = 100 = 10 ^ 3 = 1000 = 10 ^ n = 1 (n minus)

You see that the exponent angle of 10 with exponent N only changes the number. Now computers mostly use internal base 2 (bits), so in the calculation of 2 ^ 19937, the situation is set a distance in the situation of 1993-337 (counting zero positions).
As additional information: Conversion in decimal is usually a win- and split algorithm that divides numbers by the powers of ten, respectively. As you see, the actual conversion is slower than the half-million factor.

The second example is more interesting: As you are calculating M ^ N with a bigger integer meter, N is the fastest way to multiply it and store temporary results in succession. Example: 10 ^ 345

a = 10 ^ 2
b = a a = 10 ^ 4
c = b
b = 10 ^ 16
d = C * C = 10 ^ 256

Results = D C c c c c c c < / Em> C b b * 10

(can be further optimized: see Nith, seminimal algorithm)

So you only need to multiply And they can be calculated very effectively.

EDIT: The correct implementation of multiplication depends: In addition to normal school multiplication, Karatsuba, Tom-Cooke and Schoenhagen-Strasse (FFT) multiplication are used.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -