在我们的c++课程中,他们建议不要再在新项目中使用c++数组。据我所知,Stroustroup本人建议不要使用数组。但是否存在显著的性能差异?
当前回答
有时候数组确实比向量好。如果你总是在操纵别人 固定长度的对象集合,数组更好。考虑下面的代码片段:
int main() {
int v[3];
v[0]=1; v[1]=2;v[2]=3;
int sum;
int starttime=time(NULL);
cout << starttime << endl;
for (int i=0;i<50000;i++)
for (int j=0;j<10000;j++) {
X x(v);
sum+=x.first();
}
int endtime=time(NULL);
cout << endtime << endl;
cout << endtime - starttime << endl;
}
X的向量在哪里
class X {
vector<int> vec;
public:
X(const vector<int>& v) {vec = v;}
int first() { return vec[0];}
};
X的数组版本为:
class X {
int f[3];
public:
X(int a[]) {f[0]=a[0]; f[1]=a[1];f[2]=a[2];}
int first() { return f[0];}
};
数组版本的main()将更快,因为我们避免了 每次在内部循环中“new”的开销。
(此代码由我发布到comp.lang.c++)。
其他回答
为微优化人员准备的序言
记住:
程序员浪费了大量的时间去思考或担心程序中非关键部分的速度,而当考虑到调试和维护时,这些提高效率的尝试实际上会产生强烈的负面影响。我们应该忘记小的效率,大约97%的时候:过早的优化是万恶之源。但我们不应该错过这关键的3%的机会。”
(感谢metamorphosis的完整引用)
不要使用C数组来代替向量(或任何东西),因为你认为它更快,因为它应该是低级别的。你可能错了。
使用默认的向量(或者根据需要使用安全的容器),然后如果你的分析器说这是一个问题,看看你是否可以优化它,要么使用更好的算法,要么改变容器。
话虽如此,我们可以回到最初的问题。
静态/动态数组?
c++数组类比低级C数组表现得更好,因为它们了解自己很多东西,并且可以回答C数组不能回答的问题。他们能够自己打扫卫生。更重要的是,它们通常是使用模板和/或内联编写的,这意味着在调试中出现的大量代码在发布版本中分解为很少或没有代码,这意味着它们与内置的不太安全的竞争没有区别。
总而言之,它分为两类:
动态数组
使用指向malloc-ed/new-ed数组的指针最多和std::vector版本一样快,但安全性要低得多(参见litb的帖子)。
所以使用std::vector。
静态数组
最好使用静态数组:
和std::array版本一样快 更不安全。
所以使用std::array。
未初始化的内存
有时,使用一个向量而不是一个原始缓冲区招致一个可见的成本,因为向量将在构造时初始化缓冲区,而它所取代的代码没有,正如bernie by在他的回答中所说。
如果是这种情况,那么您可以通过使用unique_ptr而不是vector来处理它,或者,如果这种情况在您的代码线中不是例外,实际上编写一个类buffer_owner,它将拥有该内存,并让您轻松安全地访问它,包括调整它的大小(使用realloc?)或任何您需要的奖励。
There is no argument about which of them is the best or good to use.They both have there own use cases,they both have their pros and cons.The behavior of both containers are different in different places.One of the main difficulty with arrays is that they are fixed in size if once they are defined or initialized then you can not change values and on the other side vectors are flexible, you can change vectors value whenever you want it's not fixed in size like arrays,because array has static memory allocation and vector has dynamic memory or heap memory allocation(we can push and pop elements into/from vector) and the creator of c++ Bjarne Stroustrup said that vectors are flexible to use more than arrays.
应该避免使用带有new的c++数组(即使用动态数组)。有一个问题是,你必须跟踪它们的大小,你需要手动删除它们,并做所有的家务工作。
我们还可以很容易地在向量中插入、推入和拉出值,这在数组中是不容易做到的。
如果我们讨论性能,那么如果你处理小的值,那么你应该使用数组,如果你处理大规模的代码,那么你应该使用向量(向量比数组更擅长处理大的值)。
下面是简单的测试:
c++数组vs Vector性能测试说明
与“对向量和数组/指针的基本索引、解引用和增量操作生成的汇编代码的比较”的结论相矛盾。
数组和向量之间一定有区别。测试是这么说的…试试吧,代码就在那里……
两者之间的性能差异很大程度上取决于实现——如果你比较一个实现得很差的std::vector和一个优化的数组实现,数组会赢,但是反过来,vector会赢……
As long as you compare apples with apples (either both the array and the vector have a fixed number of elements, or both get resized dynamically) I would think that the performance difference is negligible as long as you follow got STL coding practise. Don't forget that using standard C++ containers also allows you to make use of the pre-rolled algorithms that are part of the standard C++ library and most of them are likely to be better performing than the average implementation of the same algorithm you build yourself.
也就是说,以我之见,vector在使用调试STL的调试场景中胜出,因为大多数具有适当调试模式的STL实现至少可以突出/清除人们在使用标准容器时所犯的典型错误。
哦,不要忘记数组和vector共享相同的内存布局,因此您可以使用vector将数据传递给需要基本数组的遗留C或c++代码。但是,请记住,在这种情况下,大多数赌注都是无效的,您将再次处理原始内存。
有时候数组确实比向量好。如果你总是在操纵别人 固定长度的对象集合,数组更好。考虑下面的代码片段:
int main() {
int v[3];
v[0]=1; v[1]=2;v[2]=3;
int sum;
int starttime=time(NULL);
cout << starttime << endl;
for (int i=0;i<50000;i++)
for (int j=0;j<10000;j++) {
X x(v);
sum+=x.first();
}
int endtime=time(NULL);
cout << endtime << endl;
cout << endtime - starttime << endl;
}
X的向量在哪里
class X {
vector<int> vec;
public:
X(const vector<int>& v) {vec = v;}
int first() { return vec[0];}
};
X的数组版本为:
class X {
int f[3];
public:
X(int a[]) {f[0]=a[0]; f[1]=a[1];f[2]=a[2];}
int first() { return f[0];}
};
数组版本的main()将更快,因为我们避免了 每次在内部循环中“new”的开销。
(此代码由我发布到comp.lang.c++)。