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

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

问题描述

我建立一个跨preTER和我的目标原始速度这段时间,在这(生)的情况下,我在每个时钟周期的问题。

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.

你有两个速度更快什么任何经验或信息:Vector或数组?
所有重要的是我可以访问一个元素(OP code接收)的速度,我不关心插入,分配,排序等。

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:


  • 数组是至少有点比向量更快访问一个元素i的条款。

  • 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?

没有,我不能忽视的性能差异 - 即使它的这样的小 - 我已经进行了优化,减少虚拟机的每一个方面,其执行运算codeS :)

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 ::矢量元素的访问时间是相同的元素访问时间通过提供一个普通的数组中的指针对象的(即运行时的指针值)

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平台上编译code如下

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]

访问向量元素看起来pretty大同小异。

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]

在perfromance的差异源于在第一个变种,额外 MOV 指令,它有做出额外的内存访问。

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.

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

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