Numpy performance differences depending on numerical values











up vote
5
down vote

favorite
3












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?










share|improve this question






















  • 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 than exp(-1000). So it probably comes down to some slower covergence of the exp algorithm with large numbers
    – Brenlla
    3 hours ago






  • 1




    @MattMessersmith Reasonable explanation, but nope. exp(1) is still much faster than exp(1000)
    – Brenlla
    3 hours ago















up vote
5
down vote

favorite
3












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?










share|improve this question






















  • 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 than exp(-1000). So it probably comes down to some slower covergence of the exp algorithm with large numbers
    – Brenlla
    3 hours ago






  • 1




    @MattMessersmith Reasonable explanation, but nope. exp(1) is still much faster than exp(1000)
    – Brenlla
    3 hours ago













up vote
5
down vote

favorite
3









up vote
5
down vote

favorite
3






3





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 than exp(-1000). So it probably comes down to some slower covergence of the exp algorithm with large numbers
    – Brenlla
    3 hours ago






  • 1




    @MattMessersmith Reasonable explanation, but nope. exp(1) is still much faster than exp(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












  • 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 than exp(-1000). So it probably comes down to some slower covergence of the exp algorithm with large numbers
    – Brenlla
    3 hours ago






  • 1




    @MattMessersmith Reasonable explanation, but nope. exp(1) is still much faster than exp(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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu