Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clib.conversion: Remove the as_c_contiguous function and use np.ascontiguousarray instead #3492

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 8, 2024

Description of proposed changes

np.ascontiguousarray does the same thing with the as_c_contiguous function, and is even slightly faster (see the benchmarks below).

This PR removes the as_c_contiguous function (assuming that no one is using this non-public function so the function doesn't go inot a deprecation cycle) and uses np.ascontiguousarray instead.

Benchmarks:

In [1]: from pygmt.clib.conversion import as_c_contiguous

In [2]: import numpy as np

For a small array:

In [3]: c_array = np.arange(20).reshape((4, 5))

In [4]: c_array.flags
Out[4]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False

In [5]: f_array = c_array.copy(order="F")

In [6]: f_array.flags
Out[6]:
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False

In [7]: %timeit np.ascontiguousarray(c_array)
55.1 ns ± 0.246 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [8]: %timeit as_c_contiguous(c_array)
82 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [9]: %timeit np.ascontiguousarray(f_array)
331 ns ± 1.46 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [10]: %timeit as_c_contiguous(f_array)
411 ns ± 0.539 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

For a large array:

In [12]: c_array = np.arange(2000000).reshape((4000, 500))

In [13]: f_array = c_array.copy(order="F")

In [14]: %timeit np.ascontiguousarray(c_array)
54.8 ns ± 0.289 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [15]: %timeit as_c_contiguous(c_array)
82.6 ns ± 0.788 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [16]: %timeit np.ascontiguousarray(f_array)
2.76 ms ± 21.2 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [17]: %timeit as_c_contiguous(f_array)
2.75 ms ± 15.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@seisman seisman added maintenance Boring but important stuff for the core devs run/benchmark Trigger the benchmark workflow in PRs labels Oct 8, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 8, 2024
@seisman seisman added needs review This PR has higher priority and needs review. and removed run/benchmark Trigger the benchmark workflow in PRs labels Oct 8, 2024
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This np.ascontiguousarray was available back in NumPy 1.13 (https://numpy.org/doc/1.13/reference/generated/numpy.ascontiguousarray.html) that was released on 8 Jun 2017 (and possibly earlier), just when Leo wrote this function in #100 😆

Ok with not having a deprecation cycle since there is a direct replacement. Also, I think this np.ascontiguousarray function supports the __array_function__ protocol which might help with interoperability with other array types.

@seisman
Copy link
Member Author

seisman commented Oct 8, 2024

This np.ascontiguousarray was available back in NumPy 1.13 (https://numpy.org/doc/1.13/reference/generated/numpy.ascontiguousarray.html) that was released on 8 Jun 2017 (and possibly earlier), just when Leo wrote this function in #100 😆

The function was available even in NumPy v1.0 (https://github.com/numpy/numpy/blob/v1.0/numpy/core/numeric.py#L139). I guess Leo didn't know this function at that time, just like that I didn't know it until now.

@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Oct 8, 2024
@seisman seisman merged commit 5cf7f6a into main Oct 8, 2024
26 checks passed
@seisman seisman deleted the refactor/as_c_contiguous branch October 8, 2024 13:11
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants