C++ STL:数组与向量:原始元素访问性能 [英] C++ STL: Array vs Vector: Raw element accessing performance

查看:31
本文介绍了C++ STL:数组与向量:原始元素访问性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个解释器,因为这次我的目标是原始速度,所以在这个(原始)案例中,每个时钟周期对我来说都很重要.

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 方面,数组至少比向量快一点.

这对我来说似乎很合乎逻辑.使用向量,您可以拥有数组不存在的所有安全性和控制开销.

It seems really logical for me. With vectors you have all those security and controlling overhead which doesn't exist for arrays.

(为什么)我错了吗?

不,我不能忽视性能差异 - 即使它那么 - 我已经优化并最小化了执行操作码的 VM 的所有其他部分:)

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 :)

推荐答案

std::vector 的典型实现中的元素访问时间与普通数组中的元素访问时间相同,可通过一个指针对象(即一个运行时指针值)

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<int> 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

例如,通过运行时指针值对 int 数组的典型读取访问在 x86 平台上的编译代码中如下所示

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 <result>, dword ptr [ecx + eax * 4]

对矢量元素的访问看起来几乎相同.

Access to vector element will look pretty much the same.

对作为数组对象可用的本地 int 数组的典型访问如下所示

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

// a[i]
mov eax, i
mov <result>, dword ptr [esp + <offset constant> + eax * 4]

对可用作数组对象的全局 int 数组的典型访问如下所示

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

// a[i]
mov eax, i
mov <result>, dword ptr [<absolute address constant> + eax * 4]

性能差异源于第一个变体中额外的 mov 指令,该指令必须进行额外的内存访问.

The difference in performance 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.

这篇关于C++ STL:数组与向量:原始元素访问性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆