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

前端 未结 5 1426
青春惊慌失措
青春惊慌失措 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条回答
  •  旧时难觅i
    2020-12-02 14:00

    No. Under the hood, both std::vector and C++0x std::array find the pointer to element n by adding n to the pointer to the first element.

    vector::at may be slower than array::at because the former must compare against a variable while the latter compares against a constant. Those are the functions that provide bounds checking, not operator[].

    If you mean C-style arrays instead of C++0x std::array, then there is no at member, but the point remains.

    EDIT: If you have an opcode table, a global array (such as with extern or static linkage) may be faster. Elements of a global array would be addressable individually as global variables when a constant is put inside the brackets, and opcodes are often constants.

    Anyway, this is all premature optimization. If you don't use any of vector's resizing features, it looks enough like an array that you should be able to easily convert between the two.

提交回复
热议问题