Which is faster, pointer access or reference acces

2020-08-26 03:21发布

In the example code below I allocate some instances of the struct Chunk. In the for loops I then iterate through the memory block and access the different instances by using either pointer or references, and assign them some random data.

But which for loop will execute the fastest? By my knowledge I'd say the reference-loop will be the fastest because it will require no dereferencing and has direct access to the instance in memory. How wrong / right am I?

struct Chunk {
    unsigned int a;
    float b;
    const char* c;
};

int main() {
    Chunk* pData = new Chunk[8];

    for( unsigned int i = 0; i < 8; ++i ) {
        Chunk* p = &pData[i];
        p->a = 1;
        p->b = 1.0f;
        p->c = "POINTERS";
    }

    for( unsigned int i = 0; i < 8; ++i ) {
        Chunk& r = pData[i];
        r.a = 1;
        r.b = 1.0f;
        r.c = "REFERENCES";
    }

    delete [] pData;
    return 0;
}

5条回答
姐就是有狂的资本
2楼-- · 2020-08-26 04:03

They should be the same (not about the same, but exactly the same) with any non-idiotic compiler. Under the hood, references are pointers (on 99% of compilers). There's no reason for any difference.

Pedantic: the second loop could be faster (probably not) because the data is in the cache already, but that's it. :)

查看更多
一纸荒年 Trace。
3楼-- · 2020-08-26 04:09

There should be no difference in code produced by any decent compiler.

查看更多
劫难
4楼-- · 2020-08-26 04:10

I'm tempted to say: who cares? Any difference in speed will be negligible, and you should chose the most readable. In this particular case, I would expect to see exactly the same code generated in both case. In more complicated cases, the compiler may not be able to determine later in the loop that the pointer has not been reseated, and may have to reread it. But for this to be the case, you'd have to be doing enough other things that the difference wouldn't be measurable.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-08-26 04:12

The execution time is almost the same but using references in less hectic.

查看更多
干净又极端
6楼-- · 2020-08-26 04:13

When you hesitate between two versions of the code like those in your example, you should choose the one more readable. Possible optimization of the kind you propose is supposed to be done by the compiler.

The more readable in your case is rather the version with references (actually, maybe not really more readable, but the consensus is to prefer the usage of references because pointers are more "dangerous").

But back to the effeciency: (please if someone knows assembler, better stop reading or you risk a laugh attack...) In my opinion, as the pData is alocated on the heap, the compiler will have to compile it using pointers anyway. I think that your reasoning could be kind of correct if your structure was allocated on the stack just with "Chunk data[8];". But at latest when the compiler optimizations are on the difference should be deleted anyway.

查看更多
登录 后发表回答