Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created April 6, 2017 00:56
Show Gist options
  • Save onevcat/b2e6a85d914ad394a9cfd89e8821bc22 to your computer and use it in GitHub Desktop.
Save onevcat/b2e6a85d914ad394a9cfd89e8821bc22 to your computer and use it in GitHub Desktop.
Method literal performance
import QuartzCore
var a = 1
func methodParams(file: String = #file, fun: String = #function, line: UInt = #line) -> Int {
a += 1
return a
}
func method() -> Int {
a += 1
return a
}
let startTimeParams = CACurrentMediaTime()
for _ in 1 ..< 1000000 {
_ = methodParams()
}
let endTimeParams = CACurrentMediaTime()
print("With params: \(endTimeParams - startTimeParams)")
let startTime = CACurrentMediaTime()
for _ in 1 ..< 1000000 {
_ = method()
}
let endTime = CACurrentMediaTime()
print("Without params: \(endTime - startTime)")
@onevcat
Copy link
Author

onevcat commented Apr 6, 2017

demo % swiftc -Onone demo.swift
demo % ./demo
With params: 0.0113304090000383
Without params: 0.00393622399997184

demo % swiftc -O demo.swift
demo % ./demo
With params: 0.000284381000028588
Without params: 0.000306404000184557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment