Created
September 21, 2014 11:01
-
-
Save prehistoricpenguin/231048ab886aad212bbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cstdlib> | |
#include <ctime> | |
#include <vector> | |
#include <windows.h> | |
using namespace std; | |
class Timer { | |
public: | |
Timer() : start_(), end_() { | |
} | |
void Start() { | |
QueryPerformanceCounter(&start_); | |
} | |
void Stop() { | |
QueryPerformanceCounter(&end_); | |
} | |
double GetElapsedMilliseconds() { | |
LARGE_INTEGER freq; | |
QueryPerformanceFrequency(&freq); | |
return (end_.QuadPart - start_.QuadPart) * 1000.0 / freq.QuadPart; | |
} | |
private: | |
LARGE_INTEGER start_; | |
LARGE_INTEGER end_; | |
}; | |
Timer gtimer; | |
struct VecCompareFunctor { | |
bool operator() (const int& lhs, const int& rhs) { | |
return lhs < rhs; | |
} | |
}; | |
bool VecCompareFun(const int& lhs, const int& rhs) { | |
return lhs < rhs; | |
} | |
template <typename T> | |
void test_sort(size_t loops, size_t len, T comp) { | |
double time_cost = 0.0; | |
for (size_t i = 0; i < loops; ++i) { | |
vector<int> vec; | |
for (size_t i = 0; i < len; ++i) { | |
vec.push_back(rand()); | |
} | |
gtimer.Start(); | |
std::sort(begin(vec), end(vec), comp); | |
gtimer.Stop(); | |
time_cost += gtimer.GetElapsedMilliseconds(); | |
} | |
std::cout << loops << "\t\t" << len << "\t\t" << time_cost << std::endl; | |
} | |
void test() { | |
const size_t karray_len[] = { | |
100, 1000, 10000, 100000 | |
}; | |
const size_t krun_times[] = { | |
100, 1000 | |
}; | |
std::cout << "use functor as pred" << std::endl; | |
std::cout << "loops\t\t" << "len\t\t" << "time" << std::endl; | |
for (auto len : karray_len) { | |
for (auto run : krun_times) { | |
test_sort(run, len, VecCompareFunctor()); | |
} | |
} | |
std::cout << "use function as pred" << std::endl; | |
for (auto len : karray_len) { | |
for (auto run : krun_times) { | |
test_sort(run, len, VecCompareFun); | |
} | |
} | |
} | |
int main() { | |
test(); | |
return 0; | |
} | |
/* | |
vs 2013 | |
Release Win32 | |
use functor as pred | |
loops len time | |
100 100 0.533086 | |
1000 100 6.62343 | |
100 1000 8.84935 | |
1000 1000 68.52 | |
100 10000 93.2998 | |
1000 10000 869.723 | |
100 100000 1021.6 | |
1000 100000 10312.9 | |
use function as pred | |
100 100 0.721448 | |
1000 100 7.1372 | |
100 1000 12.0756 | |
1000 1000 123.603 | |
100 10000 156.273 | |
1000 10000 1580.77 | |
100 100000 1922.36 | |
1000 100000 19511.9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment