Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created August 31, 2017 15:08
Show Gist options
  • Save TimSC/0d2dca6766972c8b482512ac1b40432e to your computer and use it in GitHub Desktop.
Save TimSC/0d2dca6766972c8b482512ac1b40432e to your computer and use it in GitHub Desktop.
Profiling tests to see if vector or sets are faster at sorting
//g++ -std=c++11 -Wall vectorvsset.cpp -g -pg -o vectorvsset
//https://stackoverflow.com/questions/24968258/which-is-approach-is-faster-vector-inserted-then-sorted-or-set
#include <time.h>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
void GenRandom(vector<int32_t> &original)
{
original.clear();
for(size_t i=0; i<10000; i++)
original.push_back(rand());
}
void TestSet(const vector<int32_t> &original)
{
set<int32_t> testset;
testset.insert(original.begin(), original.end());
}
void TestVector(const vector<int32_t> &original)
{
vector<int32_t> testvec;
testvec.insert(testvec.end(), original.begin(), original.end());
sort(testvec.begin(), testvec.end());
}
void TestVectorConvertToSet(const vector<int32_t> &original)
{
vector<int32_t> testvec;
testvec.insert(testvec.end(), original.begin(), original.end());
sort(testvec.begin(), testvec.end());
std::set<int32_t> testsec(testvec.begin(), testvec.end());
}
int main()
{
srand(time(NULL));
cout << "RAND_MAX " << RAND_MAX << endl;
vector<int32_t> original;
for(size_t i=0; i<100; i++)
{
GenRandom(original);
TestSet(original);
TestVector(original);
TestVectorConvertToSet(original);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment