fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cstdio>
  8.  
  9. const size_t LARGE_SIZE = 100000;
  10.  
  11. struct rnd {
  12. int operator()() {
  13. return rand() % LARGE_SIZE;
  14. }
  15. };
  16.  
  17. int comp( const void* a, const void* b ) {
  18. return ( *( int* )a - *( int* )b );
  19. }
  20.  
  21. int main() {
  22. int ary[LARGE_SIZE];
  23. int ary_copy[LARGE_SIZE];
  24. // generate random data
  25. std::generate( ary, ary + LARGE_SIZE, rnd() );
  26. std::copy( ary, ary + LARGE_SIZE, ary_copy );
  27. // get time
  28. std::time_t start = std::clock();
  29. // perform quick sort C using function pointer
  30. std::qsort( ary, LARGE_SIZE, sizeof( int ), comp );
  31. std::cout << "C quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "\n";
  32. // get time again
  33. start = std::clock();
  34. // perform quick sort C++ using function object
  35. std::sort( ary_copy, ary_copy + LARGE_SIZE );
  36. std::cout << "C++ quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "\n";
  37. }
Success #stdin #stdout 0.03s 4124KB
stdin
Standard input is empty
stdout
C quick-sort time elapsed: 0.021144
C++ quick-sort time elapsed: 0.007603