Millis() accuracy for very short periods

Hi all!

I've read a lot of topics about the accuracy of the millis() and even micros(), but they always discuss the deviation over long periods of time. For my project I need to measure very short periods (network latency over Wifi, ethernet) so I was thinking about:

initalTime = millis();
//do something
elapsedTime = millis() - initialTime();

Will I have enough precision? My measurements will be in the range (hopefully) of 20-500ms, and I don't care of the "real" date&time as that will be handled by the server used to correlate data.

Thanks a lot in advance.

the value of micros() changes every 4 microseconds (on a 16MHz uno-like board) and it and the value of millis() are as accurate as the oscillator (0.5% tolerance usually)

micros() rolls over about every hour (4 billion microseconds), so if you're meauring times longer than that you must use millis(). However, it also takes much longer to get the time than millis() -- millis() is (theoretically) about 15-20 cycles while micros() does a bunch of things including some branching.

I generally use micors() if it's less than 100ms, but both work.

You can use millis() or micros().
I assume that the accuracy of a network latency does not need to be accurate to the us. Even 1ms more or less is not a problem.
You have to know that the value of millis() and micros() do roll-over. So you have to handle that in your code.

Hi Erdin!

thanks for your answer. Sure, as you mention, I don't need so high accuracy. Deviations of about 5 to 10ms are allowed.

And yes, I'll handle the roll-over. Probably, once it happens millis() < initialTime the easiest thing to do is just repeat the last measure. I'll think about other ways to handle it without impact on performance.

Thanks again.

You can use unsigned long to always get a valid positive number in case of rollover.
I have made another test sketch Arduino Playground - HomePage
Try the second sketch ("Another test sketch") and see what happens.

hey, thank you very much Erdin!

I'll give it a try this evening/during the weekend and let you know the results.

iverona:
For my project I need to measure very short periods (network latency over Wifi, ethernet) so I was thinking about:

The latency of an Ethernet link should be substantially lower than you can measure with an Arduino, and the latency of a healthy WiFi link should be down to single-figure milliseconds. Most likely if you're monitoring them with an Arduino what you'll be measuring is the processing delay within your network shield and within the Arduino itself.

Hey PeterH,

actually I'll be measuring network latency, which is the sum of trip time over the network (over the internet) and the processing time on server side. So far is in the range of 20-500ms so I think millis() will be accurate enough for it...

In that case I think you have answered your own question.

Erdin did :wink: I wanted to make sure I'm not going to get weird values. I'll give it a try over the weekend.

Thanks for your support!!