Created
August 10, 2016 13:17
-
-
Save serhii-shnurenko/eaf84c4eff0eea82eeff8b6c53d319b8 to your computer and use it in GitHub Desktop.
Fibonachi number recursive and loop implementation, tests and benchmarking
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
// | |
// Created by Сергій Шнуренко on 10.08.2016. | |
// | |
#include <iostream> | |
#include "testlib.h" | |
#include "numbers.h" | |
using namespace std; | |
// benchmark tests constants | |
const int TIMES = 1000; | |
const int PARAM= 25; | |
//equality tests constants | |
const int EQUALITY_CHECKS = 10; | |
const int EQUALITY_CHECKS_RANGE = 20; | |
void equalityTests(); | |
void benchmarkTests(); | |
int main() { | |
equalityTests(); | |
benchmarkTests(); | |
return 0; | |
} | |
/** | |
* Helper function for equality testing | |
*/ | |
void equalityTests(){ | |
printf("Starting equality tests:\n"); | |
printf("Number of checks %d. Param range %d\n\n",EQUALITY_CHECKS,EQUALITY_CHECKS_RANGE); | |
srand (time(NULL)); | |
for(int i=0;i<EQUALITY_CHECKS;i++){ | |
int param = rand()%EQUALITY_CHECKS_RANGE; | |
if(!assertEquals(fibonachyRecursive,fibonachyLoop,param)){ | |
printf("FAILED with param = %d\n\n",param); | |
printf("Recursive implementation is\t%d\n",fibonachyRecursive(param)); | |
printf("Loop implementation result is\t%d\n",fibonachyLoop(param)); | |
} else{ | |
printf("PASSED with param = %d\n",param); | |
} | |
} | |
} | |
/** | |
* Helper function for benchmark testing | |
*/ | |
void benchmarkTests(){ | |
printf("\n\nStarting benchmark tests:\n"); | |
printf("Number of repeated calls %d. Param value %d\n",TIMES,PARAM); | |
printf("Recursive:\t%.0f ms\n",benchmark(fibonachyRecursive,TIMES,PARAM)); | |
printf("Loop:\t\t%.0f ms\n",benchmark(fibonachyLoop,TIMES,PARAM)); | |
} |
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
// | |
// Created by Сергій Шнуренко on 10.08.2016. | |
// | |
#include "numbers.h" | |
/** | |
* Calculates fibonachy function in recursive manner | |
* @param n parameter for function | |
* @return result of fibonachy function | |
*/ | |
long fibonachyRecursive(int n){ | |
if(n<=1){ | |
return n; | |
} else{ | |
return fibonachyRecursive(n-1)+fibonachyRecursive(n-2); | |
} | |
} | |
/** | |
* Calculates fibonachy function with for loop | |
* @param n parameter for function | |
* @return result of fibonachy function | |
*/ | |
long fibonachyLoop(int n) { | |
int prevprev=0; | |
int prev = 1; | |
if(n<=1){ | |
return n; | |
}else { | |
for(int i=0;i<=n-2;i++){ | |
int currentFibonachy = prevprev+prev; | |
prevprev = prev; | |
prev = currentFibonachy; | |
} | |
} | |
return prev; | |
} |
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
// | |
// Created by Сергій Шнуренко on 10.08.2016. | |
// | |
#ifndef HH_NUMBERS_H | |
#define HH_NUMBERS_H | |
long fibonachyRecursive(int n); | |
long fibonachyLoop(int n); | |
#endif //HH_NUMBERS_H |
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
Starting equality tests: | |
Number of checks 10. Param range 20 | |
PASSED with param = 19 | |
PASSED with param = 2 | |
PASSED with param = 6 | |
PASSED with param = 16 | |
PASSED with param = 14 | |
PASSED with param = 10 | |
PASSED with param = 12 | |
PASSED with param = 0 | |
PASSED with param = 4 | |
PASSED with param = 1 | |
Starting benchmark tests: | |
Number of repeated calls 1000. Param value 25 | |
Recursive: 822512 ms | |
Loop: 37 ms |
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
// | |
// Created by Сергій Шнуренко on 10.08.2016. | |
// | |
#include "testlib.h" | |
#include <chrono> | |
/** | |
* Benchmarking the function | |
* @param f function reference | |
* @param times times function call will be repeated | |
* @param param param with wich function cll will be repeated | |
* @return total time of all executions in milis | |
*/ | |
double benchmark(long (&f)(int),int times, int param){ | |
auto begin = std::chrono::high_resolution_clock::now(); | |
for(int i=0; i<times;i++){ | |
f(param); | |
} | |
auto end = std::chrono::high_resolution_clock::now(); | |
return (end-begin).count()/1000; | |
} | |
/** | |
* Checks for equality of functions | |
* @param f1 first function | |
* @param f2 second function | |
* @param param param for both of functions | |
* @return true if functions return the same result | |
*/ | |
bool assertEquals(long (&f1)(int), long(&f2)(int),int param){ | |
return (f1(param)==f2(param)); | |
} |
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
// | |
// Created by Сергій Шнуренко on 10.08.2016. | |
// | |
#ifndef HH_TESTLIB_H | |
#define HH_TESTLIB_H | |
double benchmark(long (&f)(int),int, int); | |
bool assertEquals(long (&f1)(int),long (&f2)(int), int ); | |
#endif //HH_TESTLIB_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment