I have the following C99 program which measures performance of simple division operations relative to addition. However, the difftime
function keeps returning 0 even though the program is clearly taking several seconds to process runAddition
and runDivision
with iterations
set to 1 billion.
#include <stdio.h>
#include <time.h>
void runAddition(long long iterations)
{
long long temp;
for (long long i = 1; i <= iterations; i++)
{
temp = temp + i;
}
}
void runDivision(long long iterations)
{
long long temp;
// Start at 1 to avoid division by 0!
for (long long i = 1; i <= iterations; i++)
{
temp = temp / i;
}
}
int main()
{
long long iterations = 1000000000;
time_t startTime;
printf("How many iterations would you like to run of each operation? ");
scanf("%d", &iterations);
printf("Running %d additions...\n", iterations);
startTime = time(NULL);
runAddition(iterations);
printf("%d additions took %f seconds\n", iterations, difftime(time(NULL), startTime));
printf("Running %d divisions...\n", iterations);
startTime = time(NULL);
runDivision(iterations);
printf("%d divisions took %f seconds\n", iterations, difftime(time(NULL), startTime));
}