C++ STL: Array vs Vector: Raw element accessing performance

前端 未结 5 1425
青春惊慌失措
青春惊慌失措 2020-12-02 13:51

I\'m building an interpreter and as I\'m aiming for raw speed this time, every clock cycle matters for me in this (raw) case.

Do you have any experience or informati

5条回答
  •  无人及你
    2020-12-02 14:09

    Element access time in a typical implementation of a std::vector is the same as element access time in an ordinary array available through a pointer object (i.e. a run-time pointer value)

    std::vector v;
    int *pa;
    ...
    v[i];
    pa[i]; 
    // Both have the same access time
    

    However, the access time to an element of an array available as an array object is better than both of the above accesses (equivalent to access through a compile-time pointer value)

    int a[100];
    ...
    a[i];
    // Faster than both of the above
    

    For example, a typical read access to an int array available through a run-time pointer value will look as follows in the compiled code on x86 platform

    // pa[i]
    mov ecx, pa // read pointer value from memory
    mov eax, i
    mov , dword ptr [ecx + eax * 4]
    

    Access to vector element will look pretty much the same.

    A typical access to a local int array available as an array object will look as follows

    // a[i]
    mov eax, i
    mov , dword ptr [esp +  + eax * 4]
    

    A typical access to a global int array available as an array object will look as follows

    // a[i]
    mov eax, i
    mov , dword ptr [ + eax * 4]
    

    The difference in perfromance arises from that extra mov instruction in the first variant, which has to make an extra memory access.

    However, the difference is negligible. And it is easily optimized to the point of being exactly the same in multiple-access context (by loading the target address in a register).

    So the statement about "arrays being a bit faster" is correct in narrow case when the array is accessible directly through the array object, not through a pointer object. But the practical value of that difference is virtually nothing.

提交回复
热议问题