问题
I am wondering if there would be any performance difference between the following in swift
:
let x = 42.42
print(floor(x))
and
let x = 42.42
extension Double {
func myFloor() -> Double {
return floor(self)
}
}
print(x.myFloor())
回答1:
The compiler inlines the code in the second case and produces the same machine code. You can see for yourself, here.
来源:https://stackoverflow.com/questions/45558276/performance-of-swift-extensions-on-primitive-types