0

I am curious if

elevation_arr = numpy.zeros([900, 1600], numpy.float32)
climate_arr = copy.deepcopy(elevation_arr)
rainfall_arr = copy.deepcopy(elevation_arr)

is faster or slower to execute than

elevation_arr = numpy.zeros([900, 1600], numpy.float32)
climate_arr = numpy.zeros([900, 1600], numpy.float32)
rainfall_arr = numpy.zeros([900, 1600], numpy.float32)
6
  • 1
    Any particular reason for using copy..deepcopy()? I think elevation_arr.copy() would anyways be equivalent to a deep copy, since the elements of elevation_arr are immutable ints. Commented Mar 23, 2019 at 22:58
  • @fountainhead I wanted to make sure the values wouldn't be references. I didn't know that immutable values would work like that. They are technically floats though aren't they? Commented Mar 23, 2019 at 23:03
  • 1
    True, they're float32s. But immutable, nevertheless, as are ints. My point is that, given an aggregate data structure (in this case numpy array) consisting of many objects, a shallow copy is good enough if the objects in the aggregation are all immutable. Only when the objects are mutable, it becomes important to make the copy a deep one, by making copies of the elements themselves. Commented Mar 23, 2019 at 23:21
  • @fountainhead Oh, I see. Thanks! I didn't know that. Commented Mar 23, 2019 at 23:23
  • For an array like yours: copy.copy(arr), copy.deepcopy(arr), arr.copy(), np.array(arr, copy=True) all time the same. deepcopy is only significantly different if the array has object dtype (same as if a list contains lists or dictionaries). Commented Mar 24, 2019 at 1:38

1 Answer 1

1

numpy_zeros performs slightly better for smaller arrays and much better for larger arrays as shown below

import copy
import numpy as np

def deep_copy():
    elevation_arr = np.zeros([900, 1600], np.float32)
    climate_arr = copy.deepcopy(elevation_arr)
    rainfall_arr = copy.deepcopy(elevation_arr)
    return 

def numpy_zeros():
    elevation_arr = np.zeros([900, 1600], np.float32)
    climate_arr = np.zeros([900, 1600], np.float32)
    rainfall_arr = np.zeros([900, 1600], np.float32)
    return

%timeit deep_copy()
# 4.13 ms ± 585 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit numpy_zeros()
# 3.01 ms ± 195 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

For a 10000 x 10000 array, following are the timings. numpy_zeros simply outperforms

%timeit deep_copy()
# 569 ms ± 50 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit numpy_zeros()
# 15.6 µs ± 1.38 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know you could time stuff like that, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.