fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <thread>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. #define ITERATIONS 2000
  10.  
  11. #define MEASURE( func ) \
  12. { \
  13. chrono::system_clock::time_point a, b; \
  14. this_thread::sleep_for( chrono::milliseconds( 2000 ) ); \
  15. a = chrono::system_clock::now(); \
  16. for ( unsigned i = 0; i < ITERATIONS; i++ ) { \
  17. for ( unsigned j = 0; j < ITERATIONS; j++ ) { \
  18. func(); \
  19. } \
  20. } \
  21. b = chrono::system_clock::now(); \
  22. cout << #func << " \t: " << chrono::duration_cast< chrono::milliseconds >( b - a ).count() << " ms." << endl; \
  23. }
  24.  
  25.  
  26. void test_qsort() {
  27. int myints[] = { 32, 71, 12, 45, 26, 80, 53, 33 };
  28. qsort( myints, sizeof( myints ) / sizeof( myints[0] ), sizeof( int ), []( const void * a, const void * b ){ return ( *(int *)a - *(int *)b ); } );
  29. }
  30.  
  31. void test_std_sort() {
  32. int myints[] = { 32, 71, 12, 45, 26, 80, 53, 33 };
  33. vector <int> myvector( myints, myints + 8 );
  34. sort( myvector.begin(), myvector.end() );
  35. }
  36.  
  37. int main()
  38. {
  39. MEASURE( test_std_sort );
  40. MEASURE( test_qsort );
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 2.22s 3432KB
stdin
Standard input is empty
stdout
test_std_sort 	: 540 ms.
test_qsort 	: 1680 ms.