Created
February 10, 2016 06:24
-
-
Save el-hoshino/868953e30dda4dd5af64 to your computer and use it in GitHub Desktop.
配列の一部範囲を取り出して新しい配列を生成する方法のパフォーマンス比較 ref: http://qiita.com/lovee/items/155a662146445ca4a79c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [Int](0 ..< 10000) | |
let number = array[1000] // number = 1000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [Int](0 ..< 10000) | |
let slice = array[1000 ..< 2000] // slice = [1000, 1001, 1002, ..., 1999] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [Int](0 ..< 10000) | |
let newArray = Array(array[1000 ..< 2000]) // newArray = [1000, 1001, 1002, ..., 1999] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [Int](0 ..< 10000) | |
let range = 4000 ..< 5000 | |
let newArray = range.map { (i) -> Int in | |
return array[i] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [Int](0 ..< 10000) | |
let range = 4000 ..< 5000 | |
let newArray = Array(array[range]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var baseArray = [Int](0 ..< 10000) | |
let childArray1Range = 3000 ..< 3050 | |
let childArray2Range = 7000 ..< 7100 | |
baseArray = baseArray.map { (i) -> Int in | |
return i * i | |
} | |
let start1 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = childArray1Range.map { (i) -> Int in | |
return baseArray[i] | |
} | |
let childArray2 = childArray2Range.map { (i) -> Int in | |
return baseArray[i] | |
} | |
} | |
print(-start1.timeIntervalSinceNow) | |
let start2 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = Array(baseArray[childArray1Range]) | |
let childArray2 = Array(baseArray[childArray2Range]) | |
} | |
print(-start2.timeIntervalSinceNow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queue = dispatch_queue_create("data", DISPATCH_QUEUE_SERIAL) | |
var baseArray = [Int](0 ..< 10000) | |
let childArray1Range = 3000 ..< 3050 | |
let childArray2Range = 7000 ..< 7100 | |
dispatch_sync(queue) { () -> Void in | |
baseArray = baseArray.map { (i) -> Int in | |
return i * i | |
} | |
} | |
func getElement(i: Int) -> Int { | |
var result = 0 | |
dispatch_sync(queue) { () -> Void in | |
result = baseArray[i] | |
} | |
return result | |
} | |
func getElements(range: Range<Int>) -> [Int] { | |
var result = [Int](range) | |
dispatch_sync(queue) { () -> Void in | |
result = Array(baseArray[range]) | |
} | |
return result | |
} | |
let start1 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = childArray1Range.map { (i) -> Int in | |
return baseArray[i] | |
} | |
let childArray2 = childArray2Range.map { (i) -> Int in | |
return baseArray[i] | |
} | |
} | |
print(-start1.timeIntervalSinceNow) | |
let start2 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = Array(baseArray[childArray1Range]) | |
let childArray2 = Array(baseArray[childArray2Range]) | |
} | |
print(-start2.timeIntervalSinceNow) | |
let start3 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = childArray1Range.map { (i) -> Int in | |
return getElement(i) | |
} | |
let childArray2 = childArray2Range.map { (i) -> Int in | |
return getElement(i) | |
} | |
} | |
print(-start3.timeIntervalSinceNow) | |
let start4 = NSDate() | |
for _ in 0 ..< 10000 { | |
let childArray1 = getElements(childArray1Range) | |
let childArray2 = getElements(childArray2Range) | |
} | |
print(-start4.timeIntervalSinceNow) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment