本文介绍了C ++阵VS矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++向量时,所花费的时间是718毫秒,
而当我使用数组,时间几乎是0毫秒。

为什么这么多的性能差异?

  INT _tmain(INT ARGC,_TCHAR *的argv [])
{
const int的大小= 10000;
clock_t表示起点,终点;
开始=时钟();
矢量<诠释> V(尺寸大小*);
的for(int i = 0; I<大小;我++)
{
对于(INT J = 0; J<大小; J ++)
{
v [我*大小+ J] = 1;
}
}
结束=时钟();
COUT<< (结束 - 开始)
<<毫秒。<< ENDL; // 718毫秒INT F = 0;
开始=时钟();
INT ARR [尺寸大小*]。
的for(int i = 0; I<大小;我++)
{
对于(INT J = 0; J<大小; J ++)
{
改编[我*大小+ J] = 1;
}
}
结束=时钟();
COUT<< (结束 - 开始)
<<毫秒。<< ENDL; // 0毫秒
返回0;
}


解决方案

您阵列ARR被分配在栈上,即编译器计算出在编译时所需的空间。在方法的开头,编译器将像插入

汇编语句

 子ESP,10000 * 10000 *的sizeof(INT)

这意味着堆栈指针(尤其)由减少10000 * 10000 *的sizeof(INT)字节来腾出的10000 整数数组。这种操作几乎是瞬间。

的载体是堆分配和堆分配是昂贵得多。当矢量分配所需的内存,它有权要求操作系统的内存和操作系统的连续块将必须执行显著工作,以找到内存这个块。

由于安德烈亚斯在评论中说,所有的时间都花在这行:

 矢量<&INT GT; V(尺寸大小*);

访问循环内的向量一样快的数组。

有关额外的概述参见例如


  • [http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap%5D%5B1%5D


  • [http://computer.howstuffworks.com/c28.htm%5D%5B2%5D


  • [http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html%5D%5B3%5D


编辑:

所有关于性能优化和编译器设置的意见后,我今天早上做了一些测试。我必须设置尺寸= 3000 所以我做了我的测量,大约有一个原条目的十分之一。在2.66GHz的至强所有的测量:


  1. 在Visual Studio 2008(不优化,运行时检查和调试运行时)调试设置矢量测试用了920毫秒,相对于0毫秒的阵列测试。

    总时间的98,48%是在花矢量::运算符[] ,即时间确实花在了运行时的检查。


  2. 通过全面优化,矢量测试,相对于0毫秒阵列需要56毫秒(与原来的条目数的十分之一)。

    矢量构造函数所需的的的应用程序运行时间61,72%。


所以,我想大家对取决于所使用的编译器设置。该任择议定书的时机提出一个优化的编译或不运行时检查的STL。

像往常一样,士气:第一个配置文件,优化第二。

when using C++ vector, time spent is 718 milliseconds,while when I use Array, time is almost 0 milliseconds.

Why so much performance difference?

int _tmain(int argc, _TCHAR* argv[])
{
const int size = 10000;
clock_t start, end;
start = clock();
vector<int> v(size*size);
for(int i = 0; i < size; i++)
{
	for(int j = 0; j < size; j++)
	{
		v[i*size+j] = 1;
	}
}
end = clock();
cout<< (end - start)
	<<" milliseconds."<<endl; // 718 milliseconds

int f = 0;
start = clock();
int arr[size*size];
for(int i = 0; i < size; i++)
{
	for(int j = 0; j < size; j++)
	{
		arr[i*size+j] = 1;
	}
}
end = clock();
cout<< ( end - start)
	<<" milliseconds."<<endl; // 0 milliseconds
return 0;
}
解决方案

Your array arr is allocated on the stack, i.e., the compiler has calculated the necessary space at compile time. At the beginning of the method, the compiler will insert an assembler statement like

sub esp, 10000*10000*sizeof(int)

which means the stack pointer (esp) is decreased by 10000 * 10000 * sizeof(int) bytes to make room for an array of 10000 integers. This operation is almost instant.

The vector is heap allocated and heap allocation is much more expensive. When the vector allocates the required memory, it has to ask the operating system for a contiguous chunk of memory and the operating system will have to perform significant work to find this chunk of memory.

As Andreas says in the comments, all your time is spent in this line:

vector<int> v(size*size);

Accessing the vector inside the loop is just as fast as for the array.

For an additional overview see e.g.

Edit:

After all the comments about performance optimizations and compiler settings, I did some measurements this morning. I had to set size=3000 so I did my measurements with roughly a tenth of the original entries. All measurements performed on a 2.66 GHz Xeon:

  1. With debug settings in Visual Studio 2008 (no optimization, runtime checks, and debug runtime) the vector test took 920 ms compared to 0 ms for the array test.

    98,48 % of the total time was spent in vector::operator[], i.e., the time was indeed spent on the runtime checks.

  2. With full optimization, the vector test needed 56 ms (with a tenth of the original number of entries) compared to 0 ms for the array.

    The vector ctor required 61,72 % of the total application running time.

So I guess everybody is right depending on the compiler settings used. The OP's timing suggests an optimized build or an STL without runtime checks.

As always, the morale is: profile first, optimize second.

这篇关于C ++阵VS矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:47