Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import itertools as it
- import timeit
- def use_list(c):
- return np.array(list(c), dtype=np.complex64)
- def use_tuple(c):
- return np.array(tuple(c), dtype=np.complex64)
- def use_fromiter(x, length, c):
- count = len(x) ** length
- base_dtype = '<c8' # 64bit (i.e. 32bit native) complex
- dtype = ','.join(length*[base_dtype])
- data = np.fromiter(c, dtype=dtype, count=count)
- data = data.view(base_dtype).reshape((-1, length))
- return data
- length = 9
- x = [1,-1,np.complex(0,1), np.complex(0,-1)]
- if __name__ == '__main__':
- t1 = timeit.timeit('use_fromiter(x, length, it.product(x, repeat=length))',
- setup='from __main__ import use_fromiter, length, x, it', number=1)
- t2 = timeit.timeit('use_list(it.product(x, repeat=length))',
- setup='from __main__ import use_list, length, x, it', number=1)
- t3 = timeit.timeit('use_tuple(it.product(x, repeat=length))',
- setup='from __main__ import use_tuple, length, x, it', number=1)
- print 'Times using a repeat of', length, ':'
- print 'Time using np.fromiter', t1, 's'
- print 'Time using lists', t2, 's'
- print 'Time using tuples', t3, 's'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement