简体   繁体   English

Swift性能和CustomStringConvertible协议

[英]Swift performance and CustomStringConvertible protocol

Here I'm trying to benchmark my distinct function, which receive array of random objects and return distinct array, by removing duplicates by phoneNumber attribute: The strange thing happens when I implement CustomStringConvertible protocol for my Person class - the time spent for the task increases 3 times (see the elapsed time in very last line) 在这里,我尝试对我的distinct函数进行基准测试,该函数通过删除phoneNumber属性中的重复项来接收随机对象的数组并返回独特的数组:当我为Person类实现CustomStringConvertible协议时,发生了奇怪的事情-花费的时间增加了3次(请参阅最后一行的经过时间)

在此处输入图片说明

I never print any Person object so I don't see obvious reason for description var being accessed for 21000 times. 我从不打印任何Person对象,因此看不到description var被访问21000次的明显原因。 Here is the result when I'm not implementing the CustomStringConvertible protocol: 当我没有实现CustomStringConvertible协议时,结果如下: 在此处输入图片说明

What is the reason for the performance drop? 性能下降的原因是什么?

I bet the playground is using the description property to display it in the results on the right side timeline view. 我敢打赌,游乐场正在使用description属性将其显示在右侧时间轴视图的结果中。 Try running this outside of a playground and see what happens. 尝试在操场外跑步,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM