Skip to content

Instantly share code, notes, and snippets.

@brunofurmon
Last active August 29, 2015 14:07
Show Gist options
  • Save brunofurmon/4d4e95a958c8dbd317d0 to your computer and use it in GitHub Desktop.
Save brunofurmon/4d4e95a958c8dbd317d0 to your computer and use it in GitHub Desktop.
// Autor: Bruno Furtado
// https://gist.github.com/brunofurmon
#include <iostream>
#include <fstream>
#include <sys/time.h>
// Método Recursivo
void potRec(int base, int exponent) {
if (exponent == 0) {
return; // result = 1
}
return potRec(base, exponent -1);
}
// Método Iterativo
void potIter(int base, int exponent) {
int result = 1;
for (int i = exponent; i > 0; i--) {
result *= base;
}
}
int main (int argc, char** argv) {
timespec timeBegin;
timespec timeEnd;
const int mainBase = 3;
const int maxExp = 1E4;
const int minExp = 1E3;
std::ofstream iterP;
std::ofstream recP;
iterP.open("ITER.csv");
recP.open("REC.csv");
for (int exp = minExp; exp < maxExp; exp++) {
clock_gettime(CLOCK_REALTIME, &timeBegin);
potIter(mainBase, exp);
clock_gettime(CLOCK_REALTIME, &timeEnd);
iterP << (timeEnd.tv_nsec - timeBegin.tv_nsec) << ", ";
//std::cout << "Tempo ITER: " << timeEnd.tv_nsec - timeBegin.tv_nsec << "\n";
clock_gettime(CLOCK_REALTIME, &timeBegin);
potRec(mainBase, exp);
clock_gettime(CLOCK_REALTIME, &timeEnd);
recP << (timeEnd.tv_nsec - timeBegin.tv_nsec) << ", ";
//std::cout << "Tempo REC: " << timeEnd.tv_nsec - timeBegin.tv_nsec << "\n";
}
iterP.close();
recP.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment