1

When using python datetime.now() I only get a resolution close to the millisecond. I need a microsecond resolution for network purposes, or at least being able to mesure the time elapsed between two received packets at a microsecond resolution.

Here is an example of the resolution I get:

from datetime import  datetime
for i in range(10):
    print(datetime.now().microsecond)    

it returns :

224000
239000
247000
255000
264000
272000
284000
295000
308000
319000

1 Answers1

0

Use the right clock for your platform; use timeit.default_timer.

datetime uses the same C API used by time.time(), which on Windows doesn't offer as much granularity as time.clock() offers. timeit.default_timer points to the right version for your platform to offer the best choice.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Forward compatibility pitfall: In Python 3.3, `timeit.default_timer = time.perf_counter`, which is useful for benchmarking but possibly not for network stuff: Undefined reference point and documented to measure "a short duration". –  May 12 '13 at 13:40