简体   繁体   English

为什么uint32_t与uint64_t的速度不同?

[英]why a uint32_t vs uint64_t speed difference?

Trying to understand something about how g++/cpu processes integers at runtime. 试图了解有关g ++ / cpu如何在运行时处理整数的信息。

I'm measuring how long the following function takes to run: 我正在测量以下功能需要运行多长时间:

template<class T>
void speedTest() {
    for(T d=0;d<4294967295u;d++)int number;
}

This simple method will run a dumb loop the max value of uint32_t many times 这个简单的方法将多次运行一个uint32_t最大值的哑循环

and when I call: 当我打电话时:

speedTest<uint32_t>();

the software takes an average of 8.15 seconds but when I call: 该软件平均需要8.15秒,但在我致电时:

speedTest<uint64_t>();

the software takes an average of 10.35 seconds. 该软件平均需要10.35秒。

Why is this happening? 为什么会这样呢?

Some possible reasons: 一些可能的原因:

  • Larger data types require more memory bandwidth in general 较大的数据类型通常需要更多的内存带宽
  • Even if that loop counter is kept inside a register, the CPU is probably taking more time to do calculations with large values, especially, if it needs multiple registers (eg if your CPU has just 32bit wide registers) 即使该循环计数器保留在寄存器中,CPU可能仍会花费更多时间来进行大数值的计算,尤其是当它需要多个寄存器时(例如,如果您的CPU只有32位宽的寄存器)
  • The compiler would need to emit extra machine instructions to emulate any type not directly supported by the CPU 编译器将需要发出额外的机器指令来模拟CPU不直接支持的任何类型
  • It also depends on optimization. 它还取决于优化。 Such a loop without side effects could be optimized out completely, regardless of int number; 不管int number; ,都可以完全优化掉这种无副作用的循环int number; (could just be for(T d=0;d<4294967295u;d++); ) (可能只是for(T d=0;d<4294967295u;d++);

You could continue your investigation/exercise by providing some assembly. 您可以通过提供一些程序集来继续进行调查/练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无符号长的类型与Windows上的uint32_t和uint64_t不同(VS2010) - Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010) 为什么在 Mac OS X 上使用 size_t 时 uint32_t 和 uint64_t 之间存在歧义? - Why is there ambiguity between uint32_t and uint64_t when using size_t on Mac OS X? 为什么在类中使用时 uint64_t 比 2 个 uint32_t 需要更多内存? 以及如何防止这种情况? - Why does an uint64_t needs more memory than 2 uint32_t's when used in a class? And how to prevent this? 将uint32_t转换为uint64_t导致不同的值? - casting uint32_t to uint64_t results in different value? 如何以类型安全的方式分配uint32_t :: max和uint64_t的最小值? - How to assign min of uint32_t::max and uint64_t in type safe manner? 使用多个uint32_t整数生成uint64_t哈希键 - Generate uint64_t hash key with several uint32_t integers 在 uint32_t 范围内变换 uint64_t 范围 - Transform uint64_t range in uint32_t range 如何让gcc警告从uint32_t到uint64_t的转换? - How to have gcc warn about conversions from uint32_t to uint64_t? 将uint64_t rdtsc值转换为uint32_t - Converting a uint64_t rdtsc value to a uint32_t c++中将一长串字符转换为uint32_t或uint64_t - convert a long string of characters to uint32_t or uint64_t in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM