#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
const size_t LARGE_SIZE = 100000;
struct rnd {
int operator()() {
return rand() % LARGE_SIZE;
}
};
int comp( const void* a, const void* b ) {
return ( *( int* )a - *( int* )b );
}
int main() {
int ary[LARGE_SIZE];
int ary_copy[LARGE_SIZE];
// generate random data
std::generate( ary, ary + LARGE_SIZE, rnd() );
std::copy( ary, ary + LARGE_SIZE, ary_copy );
// get time
std::time_t start = std::clock();
// perform quick sort C using function pointer
std::qsort( ary, LARGE_SIZE, sizeof( int ), comp );
std::cout << "C quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "\n";
// get time again
start = std::clock();
// perform quick sort C++ using function object
std::sort( ary_copy, ary_copy + LARGE_SIZE );
std::cout << "C++ quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "\n";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGNzdGRpbz4KCmNvbnN0IHNpemVfdCBMQVJHRV9TSVpFID0gMTAwMDAwOwoKc3RydWN0IHJuZCB7CiAgICBpbnQgb3BlcmF0b3IoKSgpIHsKICAgICAgICByZXR1cm4gcmFuZCgpICUgTEFSR0VfU0laRTsKICAgIH0KfTsKCmludCBjb21wKCBjb25zdCB2b2lkKiBhLCBjb25zdCB2b2lkKiBiICkgewogICAgcmV0dXJuICggKiggaW50KiApYSAtICooIGludCogKWIgKTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgYXJ5W0xBUkdFX1NJWkVdOwogICAgaW50IGFyeV9jb3B5W0xBUkdFX1NJWkVdOwogICAgLy8gZ2VuZXJhdGUgcmFuZG9tIGRhdGEKICAgIHN0ZDo6Z2VuZXJhdGUoIGFyeSwgYXJ5IC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