1 Reply
      Latest reply on Jan 24, 2017 3:07 AM by eskimo
      Audulus Level 2 Level 2 (30 points)

        I'm noticing that removeLast is very slow. Much slower than you'd expect for an O(1) removal algorithm (the constant would have to be huge).


        Here's the code I'm using for a test:


        var array = [ Int ]()
        for i in 0..<262144 {
        print ("done appending") // we get here quickly
        let n = array.count
        for _ in 0..<n {
          array.removeLast() // popLast is also slow
        print ("done")


        This takes 16 seconds on my machine. An equivalent C++ program takes .002 seconds.


        Can you repro? Any idea what's going on there?



        • Re: removeLast extremely slow
          eskimo Apple Staff Apple Staff (13,915 points)

          I can definitely reproduce the slowness in the Debug build.  Have you tried the Release build?  That seems super fast to me (so fast that I feared the compiler was optimising everything away).

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"