Swift Array removeAll() performance (slow)

Using Swift Array I found removeAll() to be very slow and not perform well.

Especially the larger the array (the more objects it contains), the slower it was.

Is there a way to achieve good performance while performing the same function as removeAll() of an array?

"= []" This method also performed the same. And the way to declare the array after making it optional had the same performance.

swift / xcode is the latest version and is being tested on iOS 15.4 / iOS 14.8 / iphone6s.

Or can't Apple make a new function that speeds up the performance of removeAll()??

Using Swift Array I found removeAll() to be very slow and not perform well.

What do you measured exactly ? What does not perform well ?

If you look at doc you see
Complexity: O(n), where n is the length of the collection.

Which means it ***** linearly with n.

So effectively, = [] is much faster.

But removeAll can take a closure with where parameter, which makes it extremely useful.

20000 ViewControllers into an Array

Is it a real case or just for test ?

I assume that when removing, the VC needs to be deallocated. That's probably what takes time.

Swift Array removeAll() performance (slow)
 
 
Q