Results
Those results were generated with vs2012 Release configuration and full optimization: (\Ox):
sort (tmp.begin(),tmp.end(),less<int>()): 399.258 milliseconds
sort (tmp.begin(),tmp.end()): 402.652 milliseconds
sort (tmp.begin(),tmp.end(), (lessFunction<int>)): 2.11581 seconds
qsort (&tmp[0],tmp.size(),sizeof(int), compare): 2.43363 seconds
Code
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <sstream>
#include <set>
#include <string>
#include <algorithm>
#include <numeric>
#include <future>
#include <functional>
using namespace std;
size_t MAX_SIZE = (1 << 28) + 1;
long long timer() {
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return li.QuadPart;
}
long long frequency() {
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
return li.QuadPart;
}
string format_time(const long long dt) {
ostringstream oss;
if ((dt) * 1000 < frequency()) {
oss << (dt) * 1000000.0 / frequency() << " microseconds";
} else if (dt < frequency()) {
oss << (dt) * 1000.0 / frequency() << " milliseconds";
} else {
oss << (dt) * 1.0 / frequency() << " seconds";
}
return oss.str();
}
void fillRandomVector (vector <int> *v)
{
srand (0);
for (auto i:*v)
i = rand();
}
template <typename T>
bool lessFunction(const T &d1, const T &d2)
{
return d1 < d2;
}
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
typedef pair<long long, string> tTiemrResult;
vector <tTiemrResult> tx;
vector <int> tmp (MAX_SIZE);
fillRandomVector (&tmp);
tx.push_back(tTiemrResult(timer(),"start"));
sort (tmp.begin(),tmp.end());
tx.push_back(tTiemrResult(timer(),"sort (tmp.begin(),tmp.end())"));
fillRandomVector (&tmp);
tx.push_back(tTiemrResult(timer(),"start"));
sort (tmp.begin(),tmp.end(),less<int>());
tx.push_back(tTiemrResult(timer(),"sort (tmp.begin(),tmp.end(),less<int>())"));
fillRandomVector (&tmp);
tx.push_back(tTiemrResult(timer(),"start"));
sort (tmp.begin(),tmp.end(), (lessFunction<int>));
tx.push_back(tTiemrResult(timer(),"sort (tmp.begin(),tmp.end(), (lessFunction<int>))"));
fillRandomVector (&tmp);
tx.push_back(tTiemrResult(timer(),"start"));
qsort (&tmp[0],tmp.size(),sizeof(int), compare);
tx.push_back(tTiemrResult(timer(),"qsort (&tmp[0],tmp.size(),sizeof(int), compare)"));
vector <tTiemrResult> tx2;
for (size_t i=1; i < tx.size() ; i++)
tx2.push_back(tTiemrResult(tx[i].first - tx[i-1].first, tx[i].second));
sort (tx2.begin(), tx2.end());
for (auto i:tx2)
cout<<i.second<<": "<< format_time (i.first)<<endl;
return 0;
}
niedziela, 24 lutego 2013
Performance of sorting procedures
Subskrybuj:
Komentarze do posta (Atom)
Brak komentarzy:
Prześlij komentarz