快速扩展原始类型的性能 [英] Performance of swift extensions on primitive types

查看:79
本文介绍了快速扩展原始类型的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道swift中的以下内容之间是否会有性能差异:

I am wondering if there would be any performance difference between the following in swift:

let x = 42.42
print(floor(x))

let x = 42.42
extension Double {
  func myFloor() -> Double {
    return floor(self)
  }
}
print(x.myFloor())

推荐答案

在第二种情况下,编译器内联代码并产生相同的机器代码. 您可以在这里亲自看到.

The compiler inlines the code in the second case and produces the same machine code. You can see for yourself, here.

这篇关于快速扩展原始类型的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆