Skip to content

Instantly share code, notes, and snippets.

@machinekoder
Created May 10, 2015 14:26
Show Gist options
  • Save machinekoder/a94393bef7bc2b688fde to your computer and use it in GitHub Desktop.
Save machinekoder/a94393bef7bc2b688fde to your computer and use it in GitHub Desktop.
Quick test for timing behavior of cos C function
/* 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;
}
@RunningLight
Copy link

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

include <inttypes.h>

Then cast the min_time and max_time so printf can handle them.

printf("min: %ju value: %f \n", (uintmax_t) min_time, min_value);
printf("max: %ju value: %f \n", (uintmax_t) max_time, max_value);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment