ascontiguousarray/asfortranarray take longer than flag check on matching order arrays
See original GitHub issueIt appears to take ~1µs to call ascontiguousarray
or asfortranarray
on an array that already matches the expected order. Simply checking the order of the array and skipping if satisfied takes ~90ns. It’s a little surprising that ascontiguousarray
or asfortranarray
take a little over an order of magnitude longer. Any thoughts why?
Reproducing code example:
In [1]: import numpy as np
In [2]: a = np.random.random((200, 300)).copy(order='C')
In [3]: %%timeit
...: np.ascontiguousarray(a)
...:
1.48 µs ± 93.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: %%timeit
...: if not a.flags.c_contiguous:
...: np.ascontiguousarray(a)
...:
86.8 ns ± 1.72 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [5]: b = np.random.random((200, 300)).copy(order='F')
In [6]: %%timeit
...: np.asfortranarray(b)
...:
1.38 µs ± 15.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [7]: %%timeit
...: if not b.flags.f_contiguous:
...: np.asfortranarray(b)
...:
87.8 ns ± 1.89 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Numpy/Python version information:
1.15.1 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 09:55:02)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
numpy.ascontiguousarray — NumPy v1.24 Manual
Return a contiguous array (ndim >= 1) in memory (C order). ... Reference object to allow the creation of arrays which are not...
Read more >NumPy: how to check leading dimension of non-owned array?
In NumPy, one can have arrays which are just pointing towards data that was allocated in a different array in order to avoid...
Read more >numeric.py - Course Hero
If you only want to check if anarray is Fortran contiguous use ``a.flags.f_contiguous`` instead.Parameters----------a : ndarrayInput array.
Read more >https://www.psych.mcgill.ca/labs/mogillab/anaconda...
If you only want to check if an array is Fortran contiguous use ``a.flags.f_contiguous`` instead. Parameters ---------- a : ndarray Input array.
Read more >Supported NumPy features - Numba
Access to Numpy arrays is very efficient, as indexing is lowered to ... It is also possible to use local or global tuples...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This has been fixed by my new argparsing code and moving these
array
“aliases” to C.Oh sorry 🤦♂️
Yep looks great! 🎉