c++ - C++ 中 CPU 使用率降低 : declaring as unsigned int or not?

标签 c++ c optimization

什么需要最多的 CPU:

int foo = 3;

或将其类型转换为 unsigned int?

unsigned int foo = 3;

最佳答案

它们在内存中都由相同的位表示。这只是如何解释位的问题,这取决于您想要的值范围。因此,两者都不是更密集。

至于处理数字,以乘法为例:我不是专家,但根据 wikipedia , 在 x86 中有用于无符号乘法 (mul) 和有符号乘法 (imul) 的操作码,这意味着它们可能需要相同的时间长度。

关于c++ - C++ 中 CPU 使用率降低 : declaring as unsigned int or not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1964595/

相关文章:

c++ - 为什么 Clang 不能优化 std::initializer_list?

c++ - 在 Microsoft Visual Studio 2010 中添加 .lib 文件

c - 从输入中读取多个值类型以在 c 中生成链表

c++ - LUA 和 C++ : How to properly use C++ function inside LUA's code

c - 为什么这个 pangram 程序没有给出正确的答案?

c - 为什么这个 C 程序符合并运行

c++ - x >= 0 是否比 x > -1 更有效?

networking - 请求http通信和过载问题

c++ - 如何在模板函数中使用不同的结构作为模板参数?

c++ - 我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?