C++ 负整数优化

发布于 2024-07-14 20:52:19 字数 77 浏览 6 评论 0原文

假设我们有一个负整数 整数a;

有没有更快的实施 -A?

我需要对此进行一些按位运算吗?

Lets say we have a negative integer say
int a;

is there a faster implementation of
-a?

Do I have to do some bitwise operation on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

兮颜 2024-07-21 20:52:19

几乎可以肯定,没有什么比编译器很可能将其转换为机器代码 NEG 指令更快的了。

如果有的话,我确信编译器会使用它。

对于二进制补码数字,您不能将其加 1,但这几乎肯定会变慢。 但我并不完全确定 C/C++ 标准是否强制要求使用二进制补码(可能是这样,我还没有检查过)。

我认为这个问题属于那些试图重写 strcpy() 等以获得更快速度的人。 这些人天真地认为 C 库 strcpy() 尚未通过使用特殊的机器代码指令(而不是大多数人第一次尝试的简单循环)进行深度优化。

您运行的性能测试是否表明您的否定花费了过长的时间?

<微妙的幽默-或者我妻子所说的-无趣>

    A NEG on a 486 (state of the art the last time I had to worry
    about clock cycles) takes 3 clock cycles (memory version,
    register only takes 1) - I'm assuming the later chips will be
    similar. On a 3Ghz CPU, that means you can do 1 billion of
    these every second. Is that not fast enough?

There's almost certainly nothing faster than the machine code NEG instruction that your compiler will most likely turn this into.

If there was, I'm sure the compiler would use it.

For a twos-complement number, you could NOT it and add 1 but that's almost certainly going to be slower. But I'm not entirely certain that the C/C++ standards mandate the use of twos-complement (they may, I haven't checked).

I think this question belongs with those that attempt to rewrite strcpy() et al to get more speed. Those people naively assume that the C library strcpy() isn't already heavily optimized by using special machine code instructions (rather than a simplistic loop that would be most people's first attempt).

Have you run performance tests which seem to indicate that your negations are taking an overly long time?

<subtle-humor-or-what-my-wife-calls-unfunny>

    A NEG on a 486 (state of the art the last time I had to worry
    about clock cycles) takes 3 clock cycles (memory version,
    register only takes 1) - I'm assuming the later chips will be
    similar. On a 3Ghz CPU, that means you can do 1 billion of
    these every second. Is that not fast enough?

</subtle-humor-or-what-my-wife-calls-unfunny>

夏末的微笑 2024-07-21 20:52:19

为了澄清 Pax 的声明,

C++ 编译器不强制使用二进制补码,除了 1 种情况。 将有符号类型转换为无符号类型时,如果数字为负数,则转换结果必须是整数的 2 补码表示形式。

简而言之,没有比 -a 更快的方法了; 即使有,也无法便携。
还要记住,过早的优化是邪恶的。 首先分析您的代码,然后解决瓶颈。

请参阅《C++ 编程语言》第三版,C.6.2.1 节。

To clarify Pax's statement,

C++ compilers are not mandated to use two's complement, except in 1 case. When you convert a signed type to an unsigned type, if the number is negative, the result of the conversion must be the 2's complement representation of the integer.

In short, there is not a faster way than -a; even if there were, it would not be portable.
Keep in mind as well that premature optimization is evil. Profile your code first and then work on the bottlenecks.

See The C++ Programming Language, 3rd Ed., section C.6.2.1.

青衫负雪 2024-07-21 20:52:19

您听说过“过早优化”这个词吗? 如果您已经优化了所有代码,并且这是唯一剩下的,那很好。 如果没有,你就是在浪费时间。

Have you ever heard the phrase "premature optimization"? If you've optimized all of your code, and this is the only thing left, fine. If not, you're wasting your time.

愁以何悠 2024-07-21 20:52:19

就 CPU 硬件而言,对数字取负是一个非常简单的操作。 我不知道有哪一个处理器执行取反所需的时间比执行任何位运算的时间更长——其中包括一些已有 30 年历史的处理器。

只是好奇,是什么让你问这个问题? 这当然不是因为您检测到瓶颈。

Negating a number is a very simple operation in terms of CPU hardware. I'm not aware of a processor that takes any longer to do negation than to do any bitwise operation - and that includes some 30 year old processors.

Just curious, what led you to ask this question? It certainly wasn't because you detected a bottleneck.

独孤求败 2024-07-21 20:52:19

也许你应该更多地考虑优化你的算法,而不是像这样的小事情。 如果这是最后要优化的事情,那么您的代码将尽可能快。

Perhaps you should think about optimizing your algorithms more-so than little things like this. If this is the last thing to optimize, your code is as fast as it's going to get.

哑剧 2024-07-21 20:52:19

所有好的答案。

如果 (-a) 有所作为,那么您已经进行了一些非常积极的性能调整。

程序的性能调整就像从湿海绵中取出水一样。 当程序第一次编写时,它是相当湿的。 只要稍加努力,您就可以从中挤出一些时间。 如果你再努力一点,你就可以把它弄干一些。

如果你真的有毅力,你可以把它放到必须放在烈日下的地方,以获取最后几个时间分子。

这就是 (-a) 可能产生影响的级别。

All good answers.

If (-a) makes a difference, you've already done some really aggressive performance tuning.

Performance tuning a program is like getting water out of a wet sponge. As a program is first written, it is pretty wet. With a little effort, you can wring some time out of it. With more effort you can dry it out some more.

If you're really persistent you can get it down to where you have to put it in the hot sun to get the last few molecules of time out of it.

That's the level at which (-a) might make a difference.

暮光沉寂 2024-07-21 20:52:19

您是否发现负数存在性能问题? 我很难想象大多数编译器都会对整数进行按位运算来否定它们。

Are you seeing a performance issue with negating numbers? I have a hard time thinking that most compilers would do a bitwise op against integers to negate them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文