Project

General

Profile

Task #4127 ยป list-sort-set-benchmark.cpp

Junxiao Shi, 01/23/2018 09:17 AM

 
// g++ -std=c++11 -O3 -o x x.cpp

#include <algorithm>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <set>

uint64_t
rdtsc()
{
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}

std::list<int> input;

int
f0()
{
int res = 0;
for (int i = 100000; i != 0; --i) {
std::list<int> l = input;
l.sort();
res = res ^ l.size();
}
return res;
}

int
f1()
{
int res = 0;
for (int i = 100000; i != 0; --i) {
std::set<int> s(input.begin(), input.end());
res = res ^ s.size();
}
return res;
}

int
main()
{
for (int i = 10; i != 0; --i) {
input.push_back(std::rand());
}

uint64_t t0 = rdtsc();
volatile int r0 = f0();
uint64_t t1 = rdtsc();
volatile int r1 = f1();
uint64_t t2 = rdtsc();

std::printf("%" PRIu64 " %" PRIu64 "\n", t1 - t0, t2 - t1);
}
    (1-1/1)