1 Reply
      Latest reply: Jan 24, 2017 3:07 AM by eskimo RSS
      Audulus Level 1 Level 1 (20 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 {
           array.append(i)
        }
        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?

         

        http://stackoverflow.com/questions/41816801/swift-array-removelast-extremely-slow

        • Re: removeLast extremely slow
          eskimo Apple Staff Apple Staff (5,995 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"