Skip to content

np.zeros_like(a) is slower than np.zeros(shape=a.shape) #9909

Open

Description

@tarcisiofischer

I was comparing the time about two ways of an array of zeros using np.zeros_like and np.zeros.
I don't know if I'm missing something, but it seems that np.zeros_like is slower and, IMO, it seems there is no reason to be... Test has been made using an Jupyter Notebook, and is shown below:

import numpy as np; a=np.zeros(shape=(1000,1000))
%timeit -n100 np.zeros(shape=a.shape)

40.7 µs ± 5.72 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

import numpy as np; a=np.zeros(shape=(1000,1000))
%timeit -n100 np.zeros_like(a)

2.18 ms ± 47.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Implementation of zeros_like is here: https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py#L138

This issue has been opened to suggest changing the whole implementation with

def zeros_like(a, dtype=None, order='K', subok=True):
  if not subok:
    return np.zeros(shape=a.shape, dtype=dtype, order=order)
  else:
    res = empty_like(a, dtype=dtype, order=order, subok=subok)
    # needed instead of a 0 to get same result as zeros for for string dtypes
    z = zeros(1, dtype=res.dtype)
    multiarray.copyto(res, z, casting='unsafe')
    return res

Activity

  1. tarcisiofischer commented on Oct 23, 2017

    @tarcisiofischer
    Author

    I just realised that there was a whole discussion about this in StackOverflow:

    https://stackoverflow.com/questions/27464039/why-the-performance-difference-between-numpy-zeros-and-numpy-zeros-like

    I can make the PR with the change, but I'll wait a bit to see if any more experienced numpy dev sees any problem with my suggestion...

  2. tadeu commented on Oct 24, 2017

    @tadeu

    It should also work with "array-like" objects, like lists of lists, e.g.:

    >>> np.zeros_like([[1,2],[3,4]])
    array([[0, 0],
           [0, 0]])

    but this can be accomplished with a tiny modification: using np.shape(a) instead of a.shape.

  3. tadeu commented on Oct 24, 2017

    @tadeu

    dtype and order should also be taken from a (unless passed explicitly, in this case they would be overridden). Example:

    >>> a=np.zeros((2,2), dtype=str) 
    >>> np.zeros_like(a)             
    array([['', ''],                 
           ['', '']],                
          dtype='<U1')
    >>> np.zeros_like(a, dtype=int)
    array([[0, 0],                 
           [0, 0]])                
  4. tarcisiofischer commented on Nov 3, 2017

    @tarcisiofischer
    Author

    Thanks for the response, @tadeu. I'll take a look soon.

  5. hmaarrfk commented on Oct 15, 2018

    @hmaarrfk
    Contributor

    I don't think your fix will address the majority of the use cases which are

    a = np.zeros((3, 3))
    b = np.zeros_like(a)
    

    where the subok parameter is never specified, but a is not a subclassing numpy it is is a numpy array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions