You would think on the face of it, that STL - with all of its abstraction and templates - would be slower than doing things the "old fashioned way". However my tests do not bear that out.
For a simple test, I wrote a small program that generated 10,000,000 (10 million) random numbers and sorted them using the inbuilt STL sort. Here it is ...
Example 11
And now the same idea using a standard array of ints, and using qsort instead...
Example 12
My figures were (on two different PCs, one running Windows NT with Microsoft Visual C++ version 6, one running Red Hat Linux with gcc (GCC) 3.2.2) ...
STL
qsort
It seems that the STL sort was faster by at least a factor of 3. So, I wouldn't be abandoning STL because you think it is too slow. :)
For a simple test, I wrote a small program that generated 10,000,000 (10 million) random numbers and sorted them using the inbuilt STL sort. Here it is ...
Example 11
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ITEMS 10000000
int main (void)
{
vector<int> v (ITEMS);
generate (v.begin (), v.end (), rand);
cout << "Starting STL sort ..." << endl;
sort (v.begin (), v.end ());
cout << "Finished STL sort." << endl << endl;
return 0;
} // end of main
And now the same idea using a standard array of ints, and using qsort instead...
Example 12
#include <iostream>
#include <algorithm>
using namespace std;
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
#define ITEMS 10000000
int v [ITEMS];
int main (void)
{
generate_n (v, ITEMS, rand);
cout << "Starting qsort ..." << endl;
qsort (v, ITEMS, sizeof(int), compare);
cout << "Finished qsort." << endl << endl;
return 0;
} // end of main
My figures were (on two different PCs, one running Windows NT with Microsoft Visual C++ version 6, one running Red Hat Linux with gcc (GCC) 3.2.2) ...
STL
- Windows with MS VC++: 15 seconds
- Linux with gcc: 5 seconds
qsort
- Windows with MS VC++: 122 seconds
- Linux with gcc: 14 seconds
It seems that the STL sort was faster by at least a factor of 3. So, I wouldn't be abandoning STL because you think it is too slow. :)