字符串格式化比将Double转换为Int更昂贵吗?

huangapple go评论62阅读模式
英文:

Is String formatting more expensive than casting a Double to an Int?

问题

我必须在我正在使用Swift编写的程序中选择两者之一,但我猜编程语言并不真的重要。

我知道字符串格式化非常昂贵,而且怀疑它比将Double转换为Int更昂贵,但我甚至都不确定,最重要的是,目前我不明白为什么会这样

此外,我在这里要做的是截断一个数字并将其打印到控制台,而不带小数部分(这意味着我不希望它看起来像这样 0.0 而是像这样 0)。最有效的方法是什么?

有人能启发我吗?

这是代码(第一行是字符串格式化,第二行是转换):

print(String(format: "%.0f", sender.value))

Int(sender.value)
英文:

I have to choose between one of the two in a program I'm writing in Swift but I guess it doesn't really matter what programming language I'm using.

I know String formatting is very expensive and suspect it to be more expensive than casting a Double to an Int, but I'm not even sure and most importantly, as of now, I don't see why this is the case.

Also, what I'm trying to do here is to truncate a number and print it to the console without a decimal part (meaning that I don't want it to appear like this 0.0 but like this 0). What is the most efficient way of doing that?

Could someone enlighten me?

Here is the code (String formatting on the first line and casting on the second):

print(String(format: "%.0f", sender.value))

Int(sender.value)

答案1

得分: 5

这不是直接回答你的问题,但希望算作“启发”。

答案是:不要过于担心微小的性能问题;大约99.999%的情况下,它们只会分散注意力,是一种危险的干扰。相反,专注于编写清晰、可读的代码,明显是正确的。一个快速但错误的程序比没有程序要糟糕得多。另一方面,一个正确但可能需要更快的程序仍然相当不错。当你的程序一直正确时,也许是时候开始考虑性能优化了。清晰正确的代码如果需要可以进行优化;"聪明"但错误的快速代码很难在不付出更多努力的情况下变得正确。(一个例子:你的"更快"版本丢弃了可能很重要的信息:双精度数的非整数部分。也许这没问题,也许不行 - 你没有说 -- 但这是人们在将性能放在正确性前面时经常犯的错误之一。)

对于你的具体问题,情况更糟,因为只有一个原因来格式化一个字符串 -- 那就是将其写入某个IO通道。IO已经比计算要昂贵得多,所以99.999%的情况下,如果在执行IO操作的途中进行一些小计算要花费5倍或10倍的代价,这实际上并不重要。

此外,关于性能的很多"知识"("我知道字符串格式化非常昂贵")只是传说,通常是错误或过时的。对非专家所谓的"昂贵"要非常非常怀疑。

所以答案是"不要担心,做清晰、正确、可读的事情就好了。"

英文:

This doesn't answer the proximate question, but hopefully counts as "enlightenment."

The answer is: just stop worrying about micro-performance issues; about 99.999% of the time, they are a dangerous distraction. Focus instead on writing clear, readable code that is obviously correct. A program that is fast but wrong is infinitely worse than no program at all. On the other hand, a program that is correct but could be faster is still pretty darn good. When you are so good that your programs are correct all the time, then maybe it is time to start thinking about improving performance. Clear correct code can be optimized if it needs it; "clever" fast-but-wrong code can rarely be made correct without much more effort. (Case in point: your "faster" version discards information that might be important: the non-integral portion of the double. Maybe that's OK, maybe it's not -- you haven't said -- but it is the sort of mistake people often make when they let performance get ahead of correctness.)

For your particular question, the dynamic is even worse, because there's only one reason to format a string -- which is to write it out to some IO channel. IO is already way more expensive that computation, so 99.999% of the time, it doesn't really matter if a small computation on the way to an IO is 5x or 10x more expensive.

Further, much of what we "know" about performance ("I know String formatting is very expensive") is just lore, and often wrong or out of date. Be very, very suspicious about what non-experts say is "expensive".

So the answer is "don't worry, do the clear, correct, readable thing."

答案2

得分: 0

当我们谈论格式化性能时,我们指的是本地化格式化。这意味着本地化十进制分隔符、分组分隔符等。想象一下使用 NumberFormatter

然而,昂贵的不是实际的格式化,而是创建格式化程序,因为这会强制应用程序加载相当复杂的Unicode信息。当我说昂贵时,我指的是相对的。只有当你每秒尝试做成百上千次时,你才会看到问题。

你正在做的只是将数字转换为字符串的基本操作。没有什么特别复杂的。根据你的用例,可能通过不同的方式节省几毫秒的时间,但否则这是一个有效的解决方案。

试图提高其性能是没有意义的。

英文:

When we are talking about the performance of formatting, we mean localized formatting. That means localizing decimal separators, grouping separators etc. Imagine the usage of NumberFormatter.

However, what is expensive is not the actual formatting but the creation of the formatter because that forces the app to load rather complex Unicode information. When I say expensive, I mean it in relative terms. You would see a problem only if you try to do it hundreds of times a second.

What you are doing is just a basic number to String conversion. Nothing overly complicated. Depending on your use case you might probably save a few milliseconds by doing it differently but otherwise this is a valid solution.

Trying to improve its performance is pointless.

huangapple
  • 本文由 发表于 2023年5月10日 23:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定