-1

I'm trying to make a C counter that will, at anytime in an ongoing program, say how many milliseconds have elapsed since the program started.

Any examples will be much appreciated. I've tried doing it on my own but run into errors when using long ints...

There might be a function already that does this, but I want to know how the function works, in C only. Can someone please help?

Jonathan Picazo
  • 975
  • 3
  • 13
  • 23
  • Please show any code that you have tried. – merlin2011 Mar 15 '14 at 01:20
  • 1
    So many duplicates... http://stackoverflow.com/questions/3557221/how-do-i-measure-time-in-c http://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c http://stackoverflow.com/questions/10154962/c-how-to-measure-time-correctly http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time?rq=1 http://stackoverflow.com/questions/13156031/measuring-time-in-c?rq=1 – bosnjak Mar 15 '14 at 01:22
  • I can't show the code. Don't have it since I deleted it to start again. – Jonathan Picazo Mar 15 '14 at 01:23
  • Delete to start again.. Must love the approach :) – bosnjak Mar 15 '14 at 01:24

1 Answers1

0

What you're looking for is a way to keep track of benchmarking. This is a simple process, and it just needs you to use the time library. The following code should be fairly simple to follow:

#include <time.h>

void foo()
{
    int sum, j;
    clock_t start, end;

    for(j = 0; j < 10; j++){
        start = clock();
        /* Do stuff here*/
        end = clock();
        printf("%.6f\n", (double) (end - start) / CLOCKS_PER_SEC);
    }
}

Yes, I know this isn't exactly what you asked for. I'm intentionally posting an answer that will make you use your brain in order to adapt it to your homework.

ciphermagi
  • 747
  • 3
  • 14
  • From the `clock()` manpage: Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes. – pat Mar 15 '14 at 01:30
  • Keep in mind that clock() behaves a bit different on different platform. On some it measures wall clock time, on others it measures time spent using the CPU. You'll have to stick with time(), which gives you seconds resolution, or more platform specific , e.g. gettimeofday() – nos Mar 15 '14 at 01:30
  • Or something like `clock_gettime` with one of `CLOCK_MONOTONIC`, `CLOCK_PROCESS_CPUTIME_ID`, or `CLOCK_THREAD_CPUTIME_ID` – pat Mar 15 '14 at 01:31
  • @nos Here is the C89 and C99 standard copied verbatim from the man page: RETURN: The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1. It is ALWAYS (in a compliant compiler) the CPU time. – ciphermagi Mar 15 '14 at 01:33
  • @ciphermagi Fair enough. The OP does not want the CPU time though, but the elapsed wall clock time since a program started. (And win32 is the one where clock() would return the wall clock time- i.e- non conformant). – nos Mar 15 '14 at 01:43
  • @nos I did state that I intentionally obfuscated the code a bit in order to provide him an opportunity to learn something. The printf() statement, also, will display wall time in ms, and if he wants it in seconds or minutes, he should be quite capable of making that change. – ciphermagi Mar 15 '14 at 01:45