Matin Rasooli
Matin Rasooli

Reputation: 3

Reshaping Arrays In Python

Why flatten() is slower than ravel() function in reshaping arrays in python?

x = np.arange(1000000)
x.shape = 100, 100, 100
%timeit x.flatten() 
%timeit x.ravel()

NumPy offers many functions and some of them can produce same results I think it depends on being Inplace and not.

Upvotes: 0

Views: 59

Answers (1)

user2357112
user2357112

Reputation: 281843

flatten always makes a copy. ravel makes a view if it can. Copying takes time.

Upvotes: 1

Related Questions