Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ STL ➜ How fast is STL?

How fast is STL?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Wed 02 Jul 2003 02:12 AM (UTC)

Amended on Wed 02 Jul 2003 04:22 AM (UTC) by Nick Gammon

Message
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


#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. :)


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


6,662 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.