np.full(size, 0) vs. np.zeros(size) vs. np.empty()

I'd use np.zeros, because of its name. I would never use the third idiom because

  1. it takes two statements instead of a single expression and

  2. it's harder for the NumPy folks to optimize. In fact, in NumPy 1.10, np.zeros is still the fastest option, despite all the optimizations to indexing:

>>> %timeit np.zeros(1e6)
1000 loops, best of 3: 804 µs per loop
>>> %timeit np.full(1e6, 0)
1000 loops, best of 3: 816 µs per loop
>>> %timeit a = np.empty(1e6); a[:] = 0
1000 loops, best of 3: 919 µs per loop

Bigger array for comparison with @John Zwinck's results:

>>> %timeit np.zeros(1e8)
100000 loops, best of 3: 9.66 µs per loop
>>> %timeit np.full(1e8, 0)
1 loops, best of 3: 614 ms per loop
>>> %timeit a = np.empty(1e8); a[:] = 0
1 loops, best of 3: 229 ms per loop

Definitely np.zeros. Not only is it the most idiomatic and common way to do this, it is also by far the fastest:

In [1]: size=100000000

In [3]: %timeit np.full(size, 0)
1 loops, best of 3: 344 ms per loop

In [4]: %timeit np.zeros(size)
100000 loops, best of 3: 8.75 µs per loop

In [5]: %timeit a = np.empty(size); a[:] = 0
1 loops, best of 3: 322 ms per loop

np.zeros is much faster if one wants to initialize an array to zeros. In the case that one just wants to initialize an array of given shape and type but doesn't care the initial entries in the array, np.empty is slightly faster.

See the following basic test results:

>>%timeit np.zeros(1000000)
7.89 µs ± 282 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

>>%timeit np.empty(1000000)
7.84 µs ± 332 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)