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 information what of the both is faster: Vector or Array?
All what matters is the speed I can access an element (opcode receiving), I don’t care about inserting, allocation, sorting, etc.
I’m going to lean myself out of the window now and say:
- Arrays are at least a bit faster than vectors in terms of accessing an element i.
It seems really logical for me. With vectors you have all those security and controlling overhead which doesn’t exist for arrays.
(Why) Am I wrong?
No, I can’t ignore the performance difference – even if it is so small – I have already optimized and minimized every other part of the VM which executes the opcodes 🙂
Element access time in a typical implementation of a
std::vectoris the same as element access time in an ordinary array available through a pointer object (i.e. a run-time pointer value)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)
For example, a typical read access to an
intarray available through a run-time pointer value will look as follows in the compiled code on x86 platformAccess to vector element will look pretty much the same.
A typical access to a local
intarray available as an array object will look as followsA typical access to a global
intarray available as an array object will look as followsThe difference in performance arises from that extra
movinstruction 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.