0

I have a function, i need to know how much time it took for execution, to my belief it should take less than 10 millisecond, so im planing to print time before entering function and after execution of function

im running on C code Linux RT and my ide is eclipse, so which function can give me time pressie to milliseconds?

Altris
  • 39
  • 3
  • You probably want `clock_gettime(CLOCK_MONOTONIC_RAW, &t)`, where `t` is a variable of type `struct timespec`. You can use this to record two timestamps, and then subtract them as described [here](https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.5/html_node/Elapsed-Time.html) – Felix G Jul 28 '20 at 11:25
  • One way to check a function's execution time is to take it into a test program and call it thousands of times and divide the overall time by the number of calls. – Weather Vane Jul 28 '20 at 11:33
  • As a side note to the processor tick printing, there might be a way to print some data about that, but there is no way to use it in a meaningful way better than the OS-provided APIs for time measurement. – Vlad Rusu Jul 28 '20 at 11:57
  • If you include ``, you can use the `__rdtsc()` intrinsic; it returns the current time stamp counter (cycle counter) as a `long long`. Personally, I prefer using `clock_gettime(CLOCK_MONOTONIC, &t)`, collect a few thousand runs, and find the *median* value. – None Jul 28 '20 at 17:06
  • You better say clock pulse for processor instead of tick. If you are asking to calc it in milliseconds, This is a duplicated question. – Amir Fo Jul 28 '20 at 18:25
  • Does this answer your question? [Execution time of C program](https://stackoverflow.com/questions/5248915/execution-time-of-c-program) – Amir Fo Jul 28 '20 at 18:27

1 Answers1

0

you can use the <time.h> header , to get the CPU time used by a task or function within a C code. use the clock_t clock(void); 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. see man pages clock:

clock_t start = clock();
/*the function i want to time it*/
foo();

clock_t end = clock();
double times = (double)(end - start) / CLOCKS_PER_SEC;
Adam
  • 2,820
  • 1
  • 13
  • 33
  • In Visual C `CLOCKS_PER_SEC` is only `1000` so `clock()` is of little use for measuring short time intervals. It's granularity is probably less than that. – Weather Vane Jul 28 '20 at 11:35