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.

np.isclose is slow

See original GitHub issue

np.isclose is very slow compared with math.isclose

Reproducing code example:

import numpy as np
import timeit
print(timeit.timeit('np.isclose(0.5, 0, atol=1e-4)', 'import numpy as np', number=10000))
# 0.487607091999962
print(timeit.timeit('math.isclose(0.5, 0, abs_tol=1e-4)', 'import math', number=10000))
# 0.007888869999987946

Error message:

Numpy/Python version information:

1.17.4 3.7.5 (default, Dec 9 2019, 22:42:26) [Clang 11.0.0 (clang-1100.0.33.12)]

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, May 10, 2020

Hehe, one of the least used and stranger features… But, its not exposed for reductions:

np.logical_and(arr1, arr2, extobj=[8192, 0, None])

It feels a bit silly that it includes the buffersize, although you could use np.geterrobj()[0], which probably incurs mainly existing overheads. (at this time, what will make that above line slower, is probably mainly the fact that extobj is passed by kwargs, and we do not use vectorcall.

@Hoiy I think we will accept PRs for some of the things you found, but there is probably only so much to be done, considering that this calls many functions all of which incur a significant overhead since they are written to be fast for arrays and not scalars. (and in general any function call has a fairly significant overhead).

0reactions
eric-wiesercommented, May 10, 2020

Apologies, I missed the import * used at the bottom (!) of this file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to understand working of np.isclose() - Stack Overflow
I am unable to understand why np.isclose() is unable to match column element-wise but is able to do it correctly for rows.
Read more >
is numpy isclose function returning bad answer?
No, it uses both an absolute and relative tolerance, and the default of rtol is nonzero, adding some more tolerance.
Read more >
How to speed up segment of code that are slowed by ... - Reddit
Having profiled the code, I know the bottleneck is the repeated call to numpy.isclose() within the function, since the profiler explicitly ...
Read more >
numpy.isclose — NumPy v1.24 Manual
For finite values, isclose uses the following equation to test whether two floating point ... np.isclose([1e10,1e-7], [1.00001e10,1e-8]) array([ True, ...
Read more >
Interpolation of an expression is slow, starting at a certain time ...
I'm slowly getting hang of how things work, but still a lot of things are ... CellType.tetrahedron, ) def bottom(x): return np.isclose(x[2], ...
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