Hello everyone,
I have written a program to answer Problem 54 on Project Euler and I have the code working successfully, but the problem I'm having is related to measuring how long it takes the program to run.
I am working on Fedora 12 running on a Virtual PC. The result I am currently getting is:
Code:
time lapsed: 0.000000
I am expecting a different result as it has to take 'some' time for it to process. Any help would be appreciated. Below is my code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//defines listed here
//function prototypes listed here
int main(void) {
char hand1[11] = {0}, hand2[11] = {0};
char *filename = "poker.txt";
FILE *fin;
int ch = 0, counter = 0, i = 0, j = 0;
static int p1_wins = 0, p2_wins = 0;
clock_t start, end;
double lapsed = 0.0;
fin = fopen(filename, "r");
if (fin == NULL) {
fprintf(stderr, "Error: unable to open %s for reading.\n", filename);
exit (1);
}
start = clock();
while ((ch = fgetc(fin)) != EOF) {
if ((ch == ' ') || (ch == '\r'))
continue;
if (counter < 10) {
hand1[i] = ch;
i++;
counter++;
}
else if (counter >= 10) {
if (ch == '\n') {
counter = 0;
i = 0;
j = 0;
determine_winner(hand1, hand2, &p1_wins, &p2_wins);
}
else {
hand2[j] = ch;
j++;
}
}
}
printf("p1_wins: %d\n", p1_wins);
printf("p2_wins: %d\n", p2_wins);
end = clock();
lapsed = (double)(end - start)/CLOCKS_PER_SEC;
printf("time lapsed: %f\n", lapsed);
return 0;
}