1

I'm trying to get the precise execution time for some code i'll use in an experiment in the lab. I was trying some simple code but i'm always getting different execution time. Can you please help me in solving this?

The code i was trying is this

#include <stdio.h> #include <unistd.h> #include <sys/time.h> int main() { long start, end; struct timeval timecheck; gettimeofday(&timecheck, NULL); start = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000; usleep(200000); // 200ms for (int i = 0; i < 10000000; ++i) {/* code */} gettimeofday(&timecheck, NULL); end = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000; printf("%ld milliseconds elapsed\n", (end - start)); return 0; } 

And the results are someting like this

227 milliseconds elapsed

231 milliseconds elapsed

228 milliseconds elapsed

6
  • Does your code do some sort of IO or other network-based calls? Such things do not have precise execution time in real-world environments./ Commented Oct 23, 2019 at 11:36
  • The results of /* code */ seem to be discarded, so that the compiler may optimize the code away. You are essentially measuring scheduler delays. Commented Oct 23, 2019 at 11:39
  • I guess if you run the code with a higher priority you will get more close run times. Commented Oct 23, 2019 at 11:41
  • 5
    If your code doesn't run as a realtime thread on a realtime OS, there is no guarantee whatsoever on the execution time. Technically the OS could decide to do whatever it wants for how long it wants, and your code may actually take 5 seconds to run. EDIT: if you're using linux, this problem can be solved by installing the PREEMPT_RT patchset, configuring the kernel for full preemption, and running your application with the SCHED_FIFO scheduling class Commented Oct 23, 2019 at 11:41
  • 1
    Your numbers differ by less than 1%. Sounds pretty consistent to me. Commented Oct 23, 2019 at 11:49

1 Answer 1

4

3-5 milliseconds deviation is not a thing to worry about. This is almost up to rounding & measures.

If the difference is bigger, then many factors can come into play here:

  1. Cache status (recently used parts of memory are faster to process)
  2. Process priority
  3. Multitasking rate (other processes may compete with you for the resources of any kind)
  4. System calls latency (are they user-space only? kernel-space?)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your explanation, but I would like to add that sometimes i get sometiong like 30 to 60 milliseconds deviation and that's do a lot of harm the reliability of my experiment, is there is way to get something more stable, or to have a constant measurements of time ?
@Yazan33, question to clarify: do you plan to measure "just your code" execution time? or OS-wide change of time between two execution points (like you do now)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.