Last active
October 22, 2017 17:26
-
-
Save phoemur/478e01258ddd88c94195db08a851c2cf 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 <iterator> | |
#include <random> | |
#include <set> | |
#include <sys/time.h> | |
#include <vector> | |
using namespace std; | |
static inline uint64_t startRDTSC (void) { | |
unsigned cycles_low, cycles_high; | |
asm volatile ("CPUID\n\t" | |
"RDTSC\n\t" | |
"mov %%edx, %0\n\t" | |
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: | |
"%rax", "%rbx", "%rcx", "%rdx"); | |
return (static_cast<uint64_t>(cycles_high) << 32) | cycles_low; | |
} | |
static inline uint64_t stopRDTSCP (void) { | |
unsigned cycles_low, cycles_high; | |
asm volatile("RDTSCP\n\t" | |
"mov %%edx, %0\n\t" | |
"mov %%eax, %1\n\t" | |
"CPUID\n\t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", | |
"%rbx", "%rcx", "%rdx"); | |
return (static_cast<uint64_t>(cycles_high) << 32) | cycles_low; | |
} | |
class CPUBenchmark { | |
public: | |
CPUBenchmark() : | |
ticktime(0) { | |
start(); | |
} | |
uint64_t ticktime; | |
void start() { | |
ticktime = startRDTSC(); | |
} | |
uint64_t stop() { | |
return stopRDTSCP() - ticktime; | |
} | |
}; | |
int sexto_maior_copia(const vector<int>& vec) | |
{ | |
//cópia para um set que já é ordenado e tem elementos unicos | |
set<int> buffer (vec.begin(), vec.end()); | |
auto it = buffer.rbegin(); | |
advance(it, 5); | |
return *it; | |
} | |
int sexto_maior_iter(const vector<int>& vec) | |
{ // Aqui iterando 6 vezes e cada vez pegando um | |
// máximo[i] < máximo[i+1] | |
int currMax=0; | |
for (unsigned j=0; j<vec.size();++j) { | |
if (vec[j] > currMax) {currMax = vec[j];} | |
} | |
for (unsigned i=0; i<5; ++i) { | |
int m = 0; | |
for (unsigned j=0; j<vec.size();++j) { | |
if (vec[j] > m && vec[j] < currMax) { | |
m = vec[j]; | |
} | |
} | |
currMax = m; | |
} | |
return currMax; | |
} | |
int main() | |
{ | |
random_device seeder; | |
mt19937 engine(seeder()); | |
uniform_int_distribution<int> dist(0, 20); | |
// Cria um vector com 20 elementos aleatórios | |
vector<int> original; | |
original.reserve(20); | |
for (unsigned i=0; i<20; ++i) { | |
original.emplace_back(dist(engine)); | |
} | |
cout << "Vetor original: " << '{'; | |
for (unsigned i=0; i<original.size(); ++i) { | |
cout << original[i]; | |
cout << (i == original.size()-1 ? '}':','); | |
} | |
cout << endl << endl << "Teste em ciclos de CPU" << endl << endl; | |
CPUBenchmark time; | |
time.start(); | |
cout << "Sexto maior elemento (cópia): " << sexto_maior_copia(original) << endl; | |
cout << "CiclosCPU: " << (time.stop()*1.0) << endl << endl; | |
time.start(); | |
cout << "Sexto maior elemento (iter) : " << sexto_maior_iter(original) << endl; | |
cout << "CiclosCPU: " << (time.stop()*1.0) << endl << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment