<div dir="auto">Might be os-specific, too. Some virtual memory management systems might special case the zeroing out of memory. Try doing the same thing with a different value than zero.</div><div class="gmail_extra"><br><div class="gmail_quote">On Dec 26, 2016 6:15 AM, "Nicolas P. Rougier" <<a href="mailto:Nicolas.Rougier@inria.fr">Nicolas.Rougier@inria.fr</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Thanks for the explanation Sebastian, makes sense.<br>
<font color="#888888"><br>
Nicolas<br>
</font><div class="elided-text"><br>
<br>
> On 26 Dec 2016, at 11:48, Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a>> wrote:<br>
><br>
> On Mo, 2016-12-26 at 10:34 +0100, Nicolas P. Rougier wrote:<br>
>> Hi all,<br>
>><br>
>><br>
>> I'm trying to understand why viewing an array as bytes before<br>
>> clearing makes the whole operation faster.<br>
>> I imagine there is some kind of special treatment for byte arrays but<br>
>> I've no clue.<br>
>><br>
><br>
> Sure, if its a 1-byte width type, the code will end up calling<br>
> `memset`. If it is not, it will end up calling a loop with:<br>
><br>
> while (N > 0) {<br>
>     *dst = output;<br>
>     *dst += 8;  /* or whatever element size/stride is */<br>
>     --N;<br>
> }<br>
><br>
> now why this gives such a difference, I don't really know, but I guess<br>
> it is not too surprising and may depend on other things as well.<br>
><br>
> - Sebastian<br>
><br>
><br>
>><br>
>> # Native float<br>
>> Z_float = np.ones(1000000, float)<br>
>> Z_int   = np.ones(1000000, int)<br>
>><br>
>> %timeit Z_float[...] = 0<br>
>> 1000 loops, best of 3: 361 µs per loop<br>
>><br>
>> %timeit Z_int[...] = 0<br>
>> 1000 loops, best of 3: 366 µs per loop<br>
>><br>
>> %timeit Z_float.view(np.byte)[...] = 0<br>
>> 1000 loops, best of 3: 267 µs per loop<br>
>><br>
>> %timeit Z_int.view(np.byte)[...] = 0<br>
>> 1000 loops, best of 3: 266 µs per loop<br>
>><br>
>><br>
>> Nicolas<br>
>> ______________________________<wbr>_________________<br>
>> NumPy-Discussion mailing list<br>
>> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
> ______________________________<wbr>_________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
<br>
______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
</div></blockquote></div><br></div>