写一道 CF 题的时候,算法明明是正确的,却一直都 TLE。最后把一个 long long 类型的数组改成了 int,竟然就 AC 了。。
这不禁引发了我的思考,int 与 long long 的运算速度不一样吗?
由于本菜鸡并没有什么计算机基础原理的知识,只好做了一个测试。当然,这个测试其实很不严谨,没有很大的参考价值。我也就图一乐,哈哈哈哈哈
仅仅是为了图一乐, 我第一次使用了 Google Benchmark 这一工具。其实挺好上手的。
| |
| |
经过多次测试,long long 类型的各种运算都比 int 慢一点。
比较专业的一个解答。详见 performance - C++ int vs long long in 64 bit machine - Stack Overflow。
这里引用相关问答:
1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data?
No- and it will probably in fact make your performance worse. For example, if you use 64-bit integers where you could have gotten away with 32-bit integers then you have just doubled the amount of data that must be sent between the processor and memory and the memory is orders of magnitude slower. All of your caches and memory buses will crap out twice as fast.
可以不用 long long 就尽量不用。最好不要使用 #define int long long 这种粗暴手段。
致谢:
碎碎念:真的有大半年没写过博客了,上次更新还是寒假呢哈哈哈。一整个学期都好忙啊,接下来就是初三了呢。
🙇
写一道 CF 题的时候,算法明明是正确的,却一直都 TLE。最后把一个 long long 类型的数组改成了 int,竟然就 AC 了。。
这不禁引发了我的思考,int 与 long long 的运算速度不一样吗?
由于本菜鸡并没有什么计算机基础原理的知识,只好做了一个测试。当然,这个测试其实很不严谨,没有很大的参考价值。我也就图一乐,哈哈哈哈哈
仅仅是为了图一乐, 我第一次使用了 Google Benchmark 这一工具。其实挺好上手的。
| |
| |
经过多次测试,long long 类型的各种运算都比 int 慢一点。
比较专业的一个解答。详见 performance - C++ int vs long long in 64 bit machine - Stack Overflow。
这里引用相关问答:
1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data?
No- and it will probably in fact make your performance worse. For example, if you use 64-bit integers where you could have gotten away with 32-bit integers then you have just doubled the amount of data that must be sent between the processor and memory and the memory is orders of magnitude slower. All of your caches and memory buses will crap out twice as fast.
可以不用 long long 就尽量不用。最好不要使用 #define int long long 这种粗暴手段。
致谢:
碎碎念:真的有大半年没写过博客了,上次更新还是寒假呢哈哈哈。一整个学期都好忙啊,接下来就是初三了呢。
🙇