Does anyone know how close I can measure time I would like to measure an event accurately to the thousands of a second?

  • I think it will do just fine most of the time. But sometimes, it will not (when some process hogs the CPU). An arduino might be more suited, since it is a real-time (single process) platform. If you could describe what you need it for, we might be able to give you some better advice as what might be more suitable. – Gerben Aug 21 '13 at 10:53

There may be some confusion about the difference between measuring time passively (how long an event takes) and creating delays (passive or active) in order to implement timing of an event.

This is measuring time:

start = gettimeofday()
[your event here]
end = gettimeofday()

Measuring the passage of time like this is very accurate; the system calls have a nanosecond granularity and will give the real time in relation to other calls.

This is implementing a passive delay:

while (1) {
    [your event]

in order to make something happen at a regular interval. With a standard linux kernel, this is subject to a latency of 10 milliseconds. In other words, if you try to do this with a granularity finer than hundredths of a second, you will not get the results you want. But with hundreds of a second it's fine -- if you wrap the nanosleep call above with gettimeofday and set the loop for 5 or 10 or 20 times per second, with no other significant processes running any given iteration will generally not be off by more than a millisecond. A millisecond is not a bad margin of error at that level, but obviously it is a bad margin of error if you want to do something 1000 times per second (you will probably end up somewhere between 500 and 1000).

Although I am sure about the accuracy of the clock, I cannot say whether actually trying to time things into the billionths of a second will work well, since on that scale there could be a variable number of nanoseconds between start and [your event] and end. However, on the milli- or even micro-second level, with no other significant processes running, it should be precise.

| improve this answer | |

I don't know for sure, seeing that I have never tried it or seen any projects that have, but I think that you will run into some issues. Any software that you run for the raspberry pi is built on a Linux operating system, which by definition is a non-real time operating system.

The issue with this comes when you have your process running in the background, and some other process decides something needs to happen. It will actually stop your program to go and do something else. This is called Time Division Multiplexing. It happens fast, normally not enough for you to even notice, but when you are looking at one thousandth of a second as your baseline, it takes quite a bit of time.

Now that being said, there are some patches and things that you can use to make Linux real-time. The Preempt-RT for example, though I don't know if it has ever, or can ever be used on a RPi. There are also software projects out there specifically for the RPi that you may want to look at. Check out https://projects.drogon.net/accurate-delays-on-the-raspberry-pi/ for an option.

| improve this answer | |
  • What do you think about something that is hard coded such as the bare metal projects? – Spencer Aug 21 '13 at 3:47
  • @Spencer At this point, I can't speak to them as I have not seen any. Could you provide a link? Another thing to think of is a a true micro controller with an Ethernet interface. Have you looked at an Arduino with an Ethernet shield? (A little more expensive, but a realtime system) – Butters Aug 21 '13 at 3:55
  • There is no advantage to a real time OS when measuring the passing of time. The clock is based on the processor frequency, which is specific and very very accurate (it's as accurate as it would be with a real time system). The advantage of a real time system is that you can guarantee events will happen in real time -- hopefully that difference is clear (marking time passively is not the same thing as implementing a delay). – goldilocks Aug 21 '13 at 7:20
  • @goldilocks What I was trying to get at here is, AFIK, there is no way to ensure you poll the GPIO at a given rate. At any point another process can jump in and slow that sequence down. Your answer does a good job at elaborating on that concept. – Butters Aug 21 '13 at 15:19
  • I guess it depends on whether what you are doing requires polling at a specific rate. For example, if you are trying to measure the rate of change of something, you get the time, read data, throw in whatever delay, then read data and get the time again. In this case, it does not matter if the delay was inaccurate -- you'll know because you've timed how long it really took, and that is the number you use for your calculations (not the time you requested). – goldilocks Aug 21 '13 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.