数组与矢量 - 性能 [英] Arrays vs. vectors - performance

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

问题描述



大家好,


我试图证明从表演中使用STL的理由

透视。作为一个起点,任何人都可以引用任何基准比较

向量与普通的静态声明数组?我也会在以后查看其他STL容器

...


此外,我会感谢任何人对适用性的评论下面用于比较矢量和数组的
小程序。一个明显的

缺点是它只使用数组/向量索引操作作为价值而不是价值...


使用VC ++ 6.0在1 GHz机器上,这个程序产生了以下

有趣的结果:


发布模式

--- ------------

数组:109秒

矢量:109秒


调试模式

-------------

数组:141秒

矢量:1657秒


在调试模式下,使用向量是* not *对齐,从性能

的角度来看,因为11.75!


我认为相同的发布模式结果并不令人惊讶。


我很想听到人们对基准的想法或参考更多

比这个小测试科学合适!


谢谢,

戴夫


#include< iostream>

#include< ctime>

#includ e< vector>


使用命名空间std;


命名空间

{

const int NUM_ELEMENTS = 100000;

const int NUM_ITERS = 100000;

};


int main()

{

int a [NUM_ELEMENTS];

time_t finish;

int i;

int j;

int k = NUM​​_ELEMENTS / 2; //一个要查找的任意元素

time_t start;

vector< int> v(NUM_ELEMENTS);


//测试数组

start = time(NULL);


for (i = 0; i!= NUM​​_ITERS; ++ i)

{

for(j = 0; j!= NUM​​_ELEMENTS; ++ j)

a [j] = i + j;

}


//尝试禁止对上述循环进行任何优化。

++ k;

cout<< a [k]<< endl;


finish = time(NULL);

cout<< 数组实现: << difftime(完成,开始)<< endl;


//测试向量

start = time(NULL);


for(i = 0; i!= NUM​​_ITERS; ++ i)

{

for(j = 0; j!= NUM​​_ELEMENTS; ++ j)

v [j] = i + j;

}


//尝试禁止对上述循环进行任何优化。

++ k;

cout<< v [k]<< endl;


finish = time(NULL);

cout<< 矢量实现: << difftime(完成,开始)<< endl;


返回0;

}


Hello all,

I''m in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing
vectors to plain old statically-declared arrays? I''ll also be looking at
the other STL containers later...

Also, I''d appreciate any comments anyone has on the suitability of the
little program below for comparing vectors and arrays. One obvious
shortcoming is that it uses array / vector indexing operations only as
lvalues, not as rvalues...

With VC++ 6.0 on a 1 GHz machine, this program produced the following
interesting results:

Release mode
---------------
Array: 109 seconds
Vector: 109 seconds

Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds

In debug mode, use of vectors is *not* justified, from a performance
perspective, by a factor of 11.75!

The identical release mode results are, I suppose, not surprising.

I''d love to hear people''s thoughts or references to benchmarks more
scientific and appropriate than this little test!

Thanks,
Dave

#include <iostream>
#include <ctime>
#include <vector>

using namespace std;

namespace
{
const int NUM_ELEMENTS = 100000;
const int NUM_ITERS = 100000;
};

int main()
{
int a[NUM_ELEMENTS];
time_t finish;
int i;
int j;
int k = NUM_ELEMENTS / 2; // An arbitrary element to look up
time_t start;
vector<int> v(NUM_ELEMENTS);

// Test the array
start = time(NULL);

for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
a[j] = i + j;
}

// Try to inhibit any optimizations of the above loops.
++k;
cout << a[k] << endl;

finish = time(NULL);
cout << "Array implementation: " << difftime(finish, start) << endl;

// Test the vector
start = time(NULL);

for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
v[j] = i + j;
}

// Try to inhibit any optimizations of the above loops.
++k;
cout << v[k] << endl;

finish = time(NULL);
cout << "vector implementation: " << difftime(finish, start) << endl;

return 0;
}

推荐答案

Dave Theese < CH ********** @ yahoo.com>写道......
"Dave Theese" <ch**********@yahoo.com> wrote...
我正试图从表现的角度来证明使用STL是合理的。作为一个起点,任何人都可以引用任何基准测试
将向量与普通的静态声明数组进行比较?我也会在以后查看其他STL容器... [...]
I''m in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old statically-declared arrays? I''ll also be looking at
the other STL containers later... [...]




(a)容器不是要超越阵列。他们总共完成了不同的任务。它们(就像许多其他东西包含在他们自己的库中的
)旨在简化开发。那是'

他们的主要名称。虽然他们确实在C ++标准中对他们施加了性能要求

,但是对于某些
$,

标准库的不同实现可能会显示不同的结果。 b $ b种类的性能测试。


(b)小程序不是任何常识的有效措施

表现为什么东西与_final_产品中的某些东西相比。

比较不同语言结构的表现而没有

系统的其余部分是愚蠢的废话。这带来了这两个

的建议:


(c)不要优化速度你不知道的东西。 (并且你不能知道
知道,直到你准备好整个系统)


(d)不要优化虽然知道慢但不会得到

执行足够的时间来影响系统的整体性能。

(再说一次,在你拥有整个系统之前你不可能知道)


如果这些东西不能说服你放弃尝试找出

为什么以及容器的数量比数组慢,请使用 www.google.com

搜索已经完成的任何性能测试。


Victor



(a) Containers are NOT intended to outperform arrays. They have totally
different task altogether. They (just like many other things wrapped
in their own libraries) are designed to simplify development. That''s
their primary designation. While they do have performance requirements
imposed on them in the C++ Standard, different implementations of the
standard library can display different results when it comes to some
kind of "performance testing".

(b) Small programs are NOT by any stretch of common sense a valid measure
for performance of something versus something in your _final_ product.
Comparing performances of different language constructs without the
rest of the system is blazing nonsense. Which brings on these two
recommendations:

(c) Do not optimise something whose speed you don''t know. (And you can''t
know until you have the entire system ready)

(d) Do not optimise something that although known as slow does not get
executed enough time to affect the overall performance of the system.
(again, you can''t know that before you have the entire system)

If those things do not convince you to give up your attempts to find out
why and by how much containers are slower than arrays, use www.google.com
to search for whatever performance testing has already been done.

Victor


Dave Theese写道:
Dave Theese wrote:
大家好,

我'我试图从表现的角度来证明使用STL是合理的。作为一个起点,任何人都可以引用任何比较
向量与普通旧静态声明数组的基准测试吗?我也会稍后看看其他STL容器......


持有它。


我想我这是一个性能怪胎,甚至我也不关心这个问题

这个问题,除非我知道我有性能问题。


有什么要求?


您知道您的应用程序是否具有性能?


如果您被要求在没有限制的情况下弄明白'已被

骗了。我建议你做一些你知道已经足够好的东西,然后你就可以使用规则 - 谎言,该死的谎言和统计数据来证明你想要的任何东西。

另外,我很感激任何人对以下
小程序是否适合比较矢量和数组的评论。


您的代码测试访问向量元素。它不会测试对象

创建和破坏或调整大小,传递或其他可能对你的代码有更多限制的事情。

Vector是一个很棒的通用工具。但是,您可以定义更多

专用版本,这些版本在* SOME *情况下运行速度要快得多。

同样,它取决于应用程序中的使用模式。


想想看。这是一种可以使用的技术。创建一个副本

(或分支)您当前拥有的代码库,运行整个

代码库并实现矢量版本。你会很快意识到

你能做到这一点。也许你可以选择一个库,如果它是一个非常庞大的代码库。然后对真实代码进行测试。如果你看到更多的b
比10%的性能命中,我会感到惊讶,除非它是一个非常复杂的

应用程序,它创建并摧毁了遍布各处的向量。


一个明显的缺点是它只使用数组/向量索引操作作为左值,而不是右值...


可能不重要。

在1 GHz机器上使用VC ++ 6.0,该程序产生了以下有趣的结果:

发布模式
- -------------
数组:109秒
矢量:109秒

调试模式
------- ------
数组:141秒
向量:1657秒

在调试模式下,使用向量是*不*对齐,从表现
透视,因子为11.75!


但是你为什么要关心调试性能?

我认为相同的发布模式结果并不令人惊讶。

我很想听到人们的想法或对基准的参考比这个小测试更科学和合适!
Hello all,

I''m in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing
vectors to plain old statically-declared arrays? I''ll also be looking at
the other STL containers later...
HOLD IT.

I think I''m a performance freak and even I don''t concern myself about
this issue unless I known I have a performance problem.

What are the requirements ?

Do you know that your application is performance constained ?

If you''ve been asked to go figure it out without constraints you''ve been
duped. I suggest you make some up that you know are good enough and you
and use the rule - lies, damn lies and statistics to prove whatever you
want.

Also, I''d appreciate any comments anyone has on the suitability of the
little program below for comparing vectors and arrays.
Your code tests accessing vector elements. It does not test object
creation and destruction or resizing, passing or other things that may
possibly be more of a constraint on your code.

Vector is a great general purpose tool. However, you can define more
specialized versions that will run considerably faster in *SOME* cases.
Again, it depends on the usage pattern in your application.

Come to think of it. Here is a technique you can use. Create a copy
(or a branch) of the codebase you currently have, run through the entire
code base and implement the vector version. You''ll be surprised how
quickly you could do this. Maybe you can pick a library if it''s a
really huge codebase. Then run a test on the real code. If you see more
than a 10% performance hit I''ll be surprised, unless it''s a very complex
app that creates and destoys vectors all over the place.

One obvious shortcoming is that it uses array / vector indexing operations only as
lvalues, not as rvalues...
Likely not significant.

With VC++ 6.0 on a 1 GHz machine, this program produced the following
interesting results:

Release mode
---------------
Array: 109 seconds
Vector: 109 seconds

Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds

In debug mode, use of vectors is *not* justified, from a performance
perspective, by a factor of 11.75!
But why do you care about debug performance ?

The identical release mode results are, I suppose, not surprising.

I''d love to hear people''s thoughts or references to benchmarks more
scientific and appropriate than this little test!




如果你有一个沉重的数值库,已经有了显着的发展。在谷歌下查找C ++矩阵库。



If you have a heavy numerical library, there has been significant
developments. Look up C++ matrix libraries under google.


>发布模式
---------------
数组:109秒
矢量:109秒

调试模式
-------------
数组:141秒
矢量:1657秒
---------------
Array: 109 seconds
Vector: 109 seconds

Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds




忘记测量调试代码的性能,但在向量的容量即将发生变化的时候加入




Forget measuring the performance of debugging-code but incorporate
the times when the vector''s capacity is about to change.


这篇关于数组与矢量 - 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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