How to store array of really huge numbers in python? -


i working huge numbers, such 150!. calculate result not problem, example
f = factorial(150) 57133839564458545904789328652610540031895535786011264182548375833179829124845398393126574488675311145377107878746854204162666250198684504466355949195922066574942592095735778929325357290444962472405416790722118445437122269675520000000000000000000000000000000000000.

but need store array n of huge numbers, in full presison. list of python can store it, slow. numpy array fast, can not handle full precision, wich required operations perform later, , have tested, number in scientific notation (float) not produce accurate result.

edit:

150! example of huge number, not mean working factorials. also, full set of numbers (not result of factorial) change on time, , need actualization , reevaluation of function wich numbers parameter, , yes, full precision required.

numpy arrays fast when can internally work simple data type can directly manipulated processor. since there no simple, native data type can store huge numbers, converted float. numpy can told work python objects slower.

here times on computer. first setup.

a python list containing first 50 factorials. b numpy array values converted float64. c numpy array storing python objects.

import numpy np import math a=[math.factorial(n) n in range(50)] b=np.array(a, dtype=np.float64) c=np.array(a, dtype=np.object)  a[30] 265252859812191058636308480000000l  b[30] 2.6525285981219107e+32  c[30] 265252859812191058636308480000000l 

now measure indexing.

%timeit a[30] 10000000 loops, best of 3: 34.9 ns per loop  %timeit b[30] 1000000 loops, best of 3: 111 ns per loop  %timeit c[30] 10000000 loops, best of 3: 51.4 ns per loop 

indexing python list fastest, followed extracting python object numpy array, , slowest extracting 64-bit float optimized numpy array.

now let's measure multiplying each element 2.

%timeit [n*2 n in a] 100000 loops, best of 3: 4.73 µs per loop  %timeit b*2 100000 loops, best of 3: 2.76 µs per loop  %timeit c*2 100000 loops, best of 3: 7.24 µs per loop 

since b*2 can take advantage of numpy's optimized array, fastest. python list takes second place. , numpy array using python objects slowest.

at least tests ran, indexing python list doesn't seem slow. slow you?


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -