0
#include <stdio.h>
#include <string.h>
#include <time.h>

int main()
{
    int i;
    FILE *fp;
    int n = 0;
    int sum = 0;
    fp = fopen("test.txt","r");
    /*count real, user, and sys time  */
    clock_t start = clock();

    while(!feof(fp)) 
    {
        fscanf(fp, "%d", &n);
        sum += n;
    }

    if(sum > 100)
        printf("Great.\n");
    else
        printf("...\n");

    printf("Sum: %d", sum);
    fclose(fp);
    /*End...*/
    clock_t end = clock();
    float seconds = (float)(end - start) / CLOCKS_PER_SEC;
    printf("\nTime: %lf\n", seconds);  

    return 0;
}

In test.txt:

1 2 3 4 5

6 7 8 9 10

Output:

sh-4.3$ main
...
Sum: 55
Time: 0.000102

My goal is to count time of real, user and sys for output.

So, is this Time considered as real time?

If yes, how to calculate user and sys time here?

John
  • 2,395
  • 15
  • 21
Patrick
  • 293
  • 1
  • 5
  • 14
  • 6
    This is not a thing that can be done using only ISO C facilities. On a Unix system you are looking for [getrusage](https://linux.die.net/man/2/getrusage). I don't know what the WIndows equivalent is. – zwol Sep 21 '16 at 21:46
  • 2
    I think you should read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/10077). – Fred Larson Sep 21 '16 at 21:49
  • `while(!feof(fp)) ` is wrong! – too honest for this site Sep 21 '16 at 21:49
  • Useful link : http://nadeausoftware.com/articles/2012/04/c_c_tip_how_measure_elapsed_real_time_benchmarking – msc Sep 22 '16 at 05:46

0 Answers0