Numpy performance differences depending on numerical values
up vote
5
down vote
favorite
I found a strange performance difference while evaluating an expression in Numpy.
I executed the following code:
import numpy as np
myarr = np.random.uniform(-1,1,[1100,1100])
and then
%timeit np.exp( - 0.5 * (myarr / 0.001)**2 )
>> 184 ms ± 301 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
and
%timeit np.exp( - 0.5 * (myarr / 0.1)**2 )
>> 12.3 ms ± 34.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
That's an almost 15x faster computation in the second case! Note that the only difference is the factor being 0.1 or 0.001.
What's the reason for this behaviour? Can I change something to make the first calculation as fast as the second?
python performance numpy
|
show 8 more comments
up vote
5
down vote
favorite
I found a strange performance difference while evaluating an expression in Numpy.
I executed the following code:
import numpy as np
myarr = np.random.uniform(-1,1,[1100,1100])
and then
%timeit np.exp( - 0.5 * (myarr / 0.001)**2 )
>> 184 ms ± 301 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
and
%timeit np.exp( - 0.5 * (myarr / 0.1)**2 )
>> 12.3 ms ± 34.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
That's an almost 15x faster computation in the second case! Note that the only difference is the factor being 0.1 or 0.001.
What's the reason for this behaviour? Can I change something to make the first calculation as fast as the second?
python performance numpy
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
2
In my system,exp
of large (negative) numbers are slower:exp(-1)
is faster thanexp(-1000)
. So it probably comes down to some slower covergence of theexp
algorithm with large numbers
– Brenlla
3 hours ago
1
@MattMessersmith Reasonable explanation, but nope.exp(1)
is still much faster thanexp(1000)
– Brenlla
3 hours ago
|
show 8 more comments
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I found a strange performance difference while evaluating an expression in Numpy.
I executed the following code:
import numpy as np
myarr = np.random.uniform(-1,1,[1100,1100])
and then
%timeit np.exp( - 0.5 * (myarr / 0.001)**2 )
>> 184 ms ± 301 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
and
%timeit np.exp( - 0.5 * (myarr / 0.1)**2 )
>> 12.3 ms ± 34.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
That's an almost 15x faster computation in the second case! Note that the only difference is the factor being 0.1 or 0.001.
What's the reason for this behaviour? Can I change something to make the first calculation as fast as the second?
python performance numpy
I found a strange performance difference while evaluating an expression in Numpy.
I executed the following code:
import numpy as np
myarr = np.random.uniform(-1,1,[1100,1100])
and then
%timeit np.exp( - 0.5 * (myarr / 0.001)**2 )
>> 184 ms ± 301 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
and
%timeit np.exp( - 0.5 * (myarr / 0.1)**2 )
>> 12.3 ms ± 34.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
That's an almost 15x faster computation in the second case! Note that the only difference is the factor being 0.1 or 0.001.
What's the reason for this behaviour? Can I change something to make the first calculation as fast as the second?
python performance numpy
python performance numpy
asked 4 hours ago
Ethunxxx
359415
359415
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
2
In my system,exp
of large (negative) numbers are slower:exp(-1)
is faster thanexp(-1000)
. So it probably comes down to some slower covergence of theexp
algorithm with large numbers
– Brenlla
3 hours ago
1
@MattMessersmith Reasonable explanation, but nope.exp(1)
is still much faster thanexp(1000)
– Brenlla
3 hours ago
|
show 8 more comments
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
2
In my system,exp
of large (negative) numbers are slower:exp(-1)
is faster thanexp(-1000)
. So it probably comes down to some slower covergence of theexp
algorithm with large numbers
– Brenlla
3 hours ago
1
@MattMessersmith Reasonable explanation, but nope.exp(1)
is still much faster thanexp(1000)
– Brenlla
3 hours ago
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
2
2
In my system,
exp
of large (negative) numbers are slower: exp(-1)
is faster than exp(-1000)
. So it probably comes down to some slower covergence of the exp
algorithm with large numbers– Brenlla
3 hours ago
In my system,
exp
of large (negative) numbers are slower: exp(-1)
is faster than exp(-1000)
. So it probably comes down to some slower covergence of the exp
algorithm with large numbers– Brenlla
3 hours ago
1
1
@MattMessersmith Reasonable explanation, but nope.
exp(1)
is still much faster than exp(1000)
– Brenlla
3 hours ago
@MattMessersmith Reasonable explanation, but nope.
exp(1)
is still much faster than exp(1000)
– Brenlla
3 hours ago
|
show 8 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415531%2fnumpy-performance-differences-depending-on-numerical-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
On OSX and NumPy 1.15.3, Python 3.7, I see 11ms vs 13ms.
– Nils Werner
3 hours ago
In Win7, NumPy 1.15.3, Python 3.6.2, I see 227 vs 22 ms
– Brenlla
3 hours ago
@jpp I'm using Ubuntu 18.04, Python 3.6.6, Numpy 1.15.4
– Ethunxxx
3 hours ago
2
In my system,
exp
of large (negative) numbers are slower:exp(-1)
is faster thanexp(-1000)
. So it probably comes down to some slower covergence of theexp
algorithm with large numbers– Brenlla
3 hours ago
1
@MattMessersmith Reasonable explanation, but nope.
exp(1)
is still much faster thanexp(1000)
– Brenlla
3 hours ago