0

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.

1
  • There aren't many numpy functions or methods that work in-place. But there is an important distinction between a view and copy. Operations that return views are faster. ravel is one; it is essentially a reshape. But sometimes even those have to return copies. Don't skip the official numpy docs, especially the beginners and essentials parts. And then keep the reference section handy for continual reference. Commented Jan 4, 2023 at 4:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.