Skip to content

Consider using asarray rather than astype #4

@esc

Description

@esc

If you do not need to cast the array, it is faster to use asarray rather than astype since asarray will be much less expensive if not cast is needed.

In [16]: a = np.arange(1e8, dtype='float32')

In [17]: %timeit a.astype('float32')
10 loops, best of 3: 200 ms per loop

In [18]: %timeit np.asarray(a, dtype='float32')
1000000 loops, best of 3: 965 ns per loop

In [19]: %timeit a.astype('float64')
1 loops, best of 3: 270 ms per loop

In [20]: %timeit np.asarray(a, dtype='float64')
1 loops, best of 3: 269 ms per loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions