Created
June 1, 2018 12:35
-
-
Save alikendarfen/378712550575ccc2bb449afcc608986c 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
// CPerf.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
int forloop_inf(int loops, int iterations) | |
{ | |
int n = 0; | |
int x = n; | |
for (int l = 0; l < loops;l++) { | |
for (int i = 0; i < iterations;i++) { | |
n++; | |
x += n; | |
} | |
} | |
return x; | |
} | |
int forloop_diff(int loops, int iterations) | |
{ | |
int n = 0; | |
int x = n; | |
for (int l = 0; l != loops;l++) { | |
for (int i = 0; i != iterations;i++) { | |
n++; | |
x += n; | |
} | |
} | |
return x; | |
} | |
double monitor(int loops, int iterations, int(*func)(int, int), int *result) | |
{ | |
clock_t start = clock(); | |
*result = func(loops, iterations); | |
clock_t stop = clock(); | |
return (double)(stop - start) / CLOCKS_PER_SEC; | |
} | |
typedef int(loop_signature)(int, int); | |
void loops_compare() | |
{ | |
int loops = 1 * 100; | |
int iterations = 1000 * 1000; | |
int result; | |
loop_signature *functions[2] = { | |
forloop_diff, | |
forloop_inf | |
}; | |
int n_rand = 1000; | |
int n[2] = { 0, 0 }; | |
double cum[2] = { 0.0, 0.0 }; | |
for (int i = 0; i < n_rand;i++) { | |
int pick = rand() % 2; | |
loop_signature *fun = functions[pick]; | |
double time = monitor(loops, iterations, fun, &result); | |
n[pick]++; | |
cum[pick] += time; | |
} | |
printf("'!=' %f (%d) / '<' %f (%d)\n", cum[0] / (double)n[0], n[0], cum[1] / (double)n[1], n[1]); | |
} | |
int main() | |
{ | |
loops_compare(); | |
char c; | |
scanf_s("\ndone%c", &c, 1); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment