Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes STL fast? [closed]

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?

like image 845
jasonline Avatar asked Feb 12 '10 17:02

jasonline


2 Answers

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.

EDIT:

As for the STL's vector class, I don't think it's necessarily faster that what you could find in, say, glibc.

like image 160
Manuel Avatar answered Sep 17 '22 01:09

Manuel


Most people's array classes increase the size by a constant increment rather than a constant factor (as the standard library requires). This means inserting an element takes roughly linear time rather than the amortized constant time for the standard library.

like image 44
Jerry Coffin Avatar answered Sep 21 '22 01:09

Jerry Coffin