简体   繁体   English

C ++中size_t的性能

[英]Performance of size_t in C++

I translated the code here into C++ as follows 我将这里的代码翻译成C ++,如下所示

#include <iostream>

using namespace std;

int t = 20;

bool is_evenly_divisible(const int a, const int b) {
    for (int i=2; i<=b; ++i) { // Line 1
        if (a%i != 0)
            return false;
    }
    return true;
}

void run() {
    int i = 10;
    while (!is_evenly_divisible(i, t)) {
        i += 2;
    }
    cout << i << endl;
}

int main(int argc, char** argv) {
    run();
    return 0;
}

With the -O3 flag on compiler g++ 4.8.1 on Mac OSX 10.8.4, I get time 0.568s user time. 在Mac OSX 10.8.4上使用编译器g ++ 4.8.1上的-O3标志,我得到0.568s用户时间。

Now if I change the counter i in Line 1 in function is_evenly_divisible to size_t, the time suddenly jumps to 1.588s. 现在,如果我将第1行中的计数器i在函数is_evenly_divisible中更改为size_t,则时间突然跳至1.588s。 This persists even if I change all of the variables to size_t, the time increases to 1.646s 即使我将所有变量更改为size_t,这仍然存在,时间增加到1.646s

What's happening? 发生了什么? Shouldn't size_t increase performance rather than decreasing it as it's a more specific type than int? size_t不应该提高性能而不是降低性能,因为它比int更具体吗?

int is usually the fastest all-round type. int通常是最快的全能类型。 This property isn't mandated by the standard, but it's usually the case for today's platforms. 此属性不是标准规定的,但通常情况下今天的平台。 We've also got things like cstdint's int_fast32_t which is more properly guaranteed to be the fastest type that can hold at least 32 bits -- highly recommend them for perf-sensitive code! 我们还得到了像cstdint的int_fast32_t这样的int_fast32_t ,它更适合保证成为能保持至少32位的最快类型 - 强烈推荐它们用于性能敏感的代码!

size_t isn't intended to give a fast integer. size_t不是为了给出一个快速整数。 Its purpose is to provide an integer that can hold the size of the largest sized object your platform's address space can contain. 其目的是提供一个整数,该整数可以保存平台地址空间可以包含的最大对象的大小。 Typically size_t is equivalent to the largest "native" integer your CPU supports, but it doesn't have to be. 通常, size_t相当于CPU支持的最大“本机”整数,但并非必须如此。

I'm going to guess that you're on a 64-bit platform. 我猜你是在64位平台上。 Generally a 64-bit platform has about equal perf for 32-bit and 64-bit ops, but you've hit the one place they typically aren't: div/mod can actually be 2-3x slower on a 64-bit integer. 通常,64位平台对于32位和64位操作具有大约相等的性能,但是您已经达到了通常不是的一个位置:div / mod实际上在64位整数上可能慢2-3倍。 In this case if int is 32-bit and size_t is 64-bit, it explains the issue nicely. 在这种情况下,如果int是32位且size_t是64位,它很好地解释了这个问题。

See Agner Fog's instruction tables document for more info. 有关详细信息,请参阅Agner Fog的说明表文档。 For Intel's Sandy Bridge platform, it shows 32-bit div has a latency of 20-28 cycles while 64-bit div takes 30-94 cycles. 对于英特尔的Sandy Bridge平台,它显示32位div的延迟为20-28个周期,而64位div则需要30-94个周期。

Size of size_t is implementation-defined and if you are running 64-bit machine most of the compilers would make it 8-byte long. size_t的大小是实现定义的,如果你运行64位机器,大多数编译器会使它长8字节。

Operations with 8-byte integers are usually slower than with 4-byte analogs. 使用8字节整数的操作通常比使用4字节模拟的操作慢。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM