gpt4 book ai didi

ios - Swift 性能和 CustomStringConvertible 协议(protocol)

转载 作者:可可西里 更新时间:2023-11-01 00:52:22 24 4
gpt4 key购买 nike

在这里,我尝试对我的 distinct 函数进行基准测试,该函数接收随机对象数组并返回不同的数组,方法是通过 phoneNumber 属性删除重复项:当我为我的 Person 类实现 CustomStringConvertible 协议(protocol)时,奇怪的事情发生了——任务花费的时间增加了 3 倍(请参阅最后一行的耗时)

enter image description here

我从不打印任何 Person 对象,所以我看不到 description var 被访问 21000 次的明显原因。以下是我未实现 CustomStringConvertible 协议(protocol)时的结果: enter image description here

性能下降的原因是什么?

最佳答案

我打赌 playground 正在使用 description 属性将其显示在右侧时间轴 View 的结果中。尝试在 Playground 外运行它,看看会发生什么。

关于ios - Swift 性能和 CustomStringConvertible 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33020705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com