简体   繁体   中英

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)

在此处输入图片说明

I never print any Person object so I don't see obvious reason for description var being accessed for 21000 times. Here is the result when I'm not implementing the CustomStringConvertible protocol: 在此处输入图片说明

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. Try running this outside of a playground and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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