question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ascontiguousarray/asfortranarray take longer than flag check on matching order arrays

See original GitHub issue

It 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:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Nov 5, 2021

This has been fixed by my new argparsing code and moving these array “aliases” to C.

0reactions
jakirkhamcommented, Nov 5, 2021

Oh sorry 🤦‍♂️

Yep looks great! 🎉

In [1]: import numpy as np

In [2]: a = np.random.random((200, 300)).copy(order='C')

In [3]: %%timeit
   ...: np.ascontiguousarray(a)
   ...: 
   ...: 
70.3 ns ± 0.192 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: b = np.random.random((200, 300)).copy(order='F')

In [5]: %%timeit
   ...: np.asfortranarray(b)
   ...: 
   ...: 
68.4 ns ± 0.427 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found