BUG: Large overhead in some random functions
See original GitHub issueThis issue is extracted from gitter (by @andyfaff – I saw it only randomly, so creating this before we forget).
Some random generator functions such as random()
have a huge overhead compared to their legacy RandomState
versions:
rs = np.random.RandomState()
rg = np.random.default_rng()
%timeit rs.random()
# 432 ns ± 9.18 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit rg.random()
# 5.19 µs ± 61.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
The reason for this is the support of the dtype=
keyword argument. A secondary reason (and maybe second speed issue) is that np.dtype.name
is a very slow operations (it could plausibly be cached).
However, the solution here will be to simply avoid the whole np.dtype(dtype).name
construct as much as possible and maybe adding a fastpath for the case when dtype
is not used. np.dtype(dtype).type is np.float64
may be a solution, or dtype is np.float64 or np.dtype(dtype) is np.float64
to speed up the default branch.
Checking that we have benchmarks – or adding simple small array ones in a first commit – for this would be good when this is fixed.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Agreed, hopefully the slowness is enough to motivate people not to use it 😉
@mattip Thank you so much 😃 I tried to follow mentioned comments, in case of any potential improvements please let me know. PR: https://github.com/numpy/numpy/pull/15511