Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save hiroshi-maybe/4e6c1971fa6842586b20 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshi-maybe/4e6c1971fa6842586b20 to your computer and use it in GitHub Desktop.
Swift 2.0 benchmark
import XCTest
//@testable import Playground
class StructVsClassTests: XCTestCase {
let times = 1000000
// 0.023 sec
// 0.002 sec on Release build
func testPerformanceStructConstructor() {
// This is an example of a performance test case.
self.measureBlock {
for _ in 0...self.times {
_ = MyStruct(x: 10)
}
}
}
// 0.135 sec
// 0.116 sec on Release build
func testPerformanceClassConstructor() {
// This is an example of a performance test case.
self.measureBlock {
//var x: MyStruct
for _ in 0...self.times {
_ = MyClass(x: 10)
}
}
}
// 0.024 sec
// 0.001 sec on Release build
func testPerformanceStructMethodCall() {
// This is an example of a performance test case.
self.measureBlock {
let x = MyStruct(x: 10)
for _ in 0...self.times {
_ = x.doubledX()
}
}
}
// 0.027 sec
// 0.003 sec on Release build
func testPerformanceClassMethodCall() {
// This is an example of a performance test case.
self.measureBlock {
let x = MyClass(x: 10)
for _ in 0...self.times {
_ = x.doubledX()
}
}
}
// 0.021 sec
// 0.000 sec on Release build
func testPerformanceStructPropertyAccess() {
// This is an example of a performance test case.
self.measureBlock {
let x = MyStruct(x: 10)
for _ in 0...self.times {
_ = x.x
}
}
}
// 0.023 sec
// 0.002 sec on Release build
func testPerformanceClassPropertyAccess() {
// This is an example of a performance test case.
self.measureBlock {
let x = MyClass(x: 10)
for _ in 0...self.times {
_ = x.x
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment