Created
May 10, 2015 14:26
-
-
Save machinekoder/a94393bef7bc2b688fde to your computer and use it in GitHub Desktop.
Quick test for timing behavior of cos C function
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
/* Program to demonstrate time taken by function cos() */ | |
#include <stdio.h> | |
#include <time.h> | |
#include <math.h> | |
// The main program calls fun() and measures time taken by fun() | |
int main() | |
{ | |
// Calculate the time taken by fun() | |
clock_t start; | |
clock_t end; | |
clock_t used; | |
clock_t min_time; | |
clock_t max_time; | |
double max_value; | |
double min_value; | |
const double start_value = -100.0; | |
const double end_value = 100.0; | |
const double step = 0.00001; | |
double value; | |
min_time = 1000000; | |
max_time = 0; | |
value = start_value; | |
while (value < end_value) { | |
start = clock(); | |
cos(value); | |
end = clock(); | |
used = end - start; | |
if (used < min_time) { | |
min_time = used; | |
min_value = value; | |
} | |
if (used > max_time) { | |
max_time = used; | |
max_value = value; | |
} | |
value += step; | |
} | |
printf("min: %u value: %f \n", min_time, min_value); | |
printf("max: %u value: %f \n", max_time, max_value); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't had a chance to think about what we're actually trying to measure here, but there's a problem with printing
clock_t
values with the format specifier"%u"
.Maybe add
Then cast the min_time and max_time so printf can handle them.