import Foundation let word = String(repeating: "abcde", count: 200_000) // 1,000,000 characters let k = 999_999 // Access near the end // MARK: - String.Index approach let startIndexMethod = CFAbsoluteTimeGetCurrent() let index = word.index(word.startIndex, offsetBy: k) let charFromIndex = word[index] let endIndexMethod = CFAbsoluteTimeGetCurrent() let indexDuration = endIndexMethod - startIndexMethod print("Character using String.Index: \(charFromIndex)") print("Time using String.Index: \(indexDuration) seconds") // MARK: - Array(word)[k] approach let startArrayMethod = CFAbsoluteTimeGetCurrent() let arrayWord = Array(word) let charFromArray = arrayWord[k] let endArrayMethod = CFAbsoluteTimeGetCurrent() let arrayDuration = endArrayMethod - startArrayMethod print("Character using Array: \(charFromArray)") print("Time using Array(word): \(arrayDuration) seconds")