gpt4 book ai didi

c++ - 为什么使用 unsigned short 比使用 int 慢?

转载 作者:行者123 更新时间:2023-12-01 14:03:54 24 4
gpt4 key购买 nike

this网页,对 unsigned short state 的评论:

Used to reduce memory usage (although the resulting executable may belarger and probably slower as compared to using int


为什么是这样?

最佳答案

我认为“可能更慢”这个词太难了。
一个理论事实是:
计算至少用 int 完成尺寸,例如

short a = 5;
short b = 10;
short c = a + b;
此代码片段包含 3 个隐式转换。 ab转换为 int并补充道。结果从 int 转换回来至 short .你无法避免它。 C++ 中的算术至少使用 int尺寸。这称为整数提升。
实际上,编译器会避免大多数转换。优化器也会移除很多转化。

关于c++ - 为什么使用 unsigned short 比使用 int 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62554173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com