问题描述
如果一个实现数组类的方式是通常实现的,它的性能比它的STL等效,比如一个矢量。那么什么使STL容器/算法快速?
If one implements an array class the way it is commonly implemented, its performance is slower compared to its STL equivalent like a vector. So what makes STL containers/algorithms fast?
推荐答案
STL算法如 for_each
获取可以轻松内联的函数对象。另一方面,C使用对编译器更难以优化的函数指针。
STL algorithms like for_each
take function objects that can be easily inlined. C, on the other hand, uses function pointers which are much more difficult for the compiler to optimize.
这使得一些算法,如排序,比较器函数必须被调用许多次。
This makes a big difference in some algorithms like sorting in which the comparer function must be called many times.
维基百科有,以备您感兴趣。
Wikipedia has some more information in case you are interested.
编辑:
至于STL的向量类,认为它必须更快,你可以找到,比如说,glibc。
As for the STL's vector class, I don't think it's necessarily faster that what you could find in, say, glibc.
这篇关于什么使STL快速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!