Last active
August 29, 2015 14:07
-
-
Save brunofurmon/4d4e95a958c8dbd317d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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