*Published on 2015-06-27.*

What is the precision of datetime in Python? The documentation says

Return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).

It goes on further to say

Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.

So, the answer is that it depends.

Let's try to figure out what it looks like on Windows using Python 3.4. For reference:

In [1]:

```
import sys
print(sys.version)
```

Let's create a series of datetime objects that we can analyze to find out how far apart they are:

In [2]:

```
import datetime
dates = []
for _ in range(10000000):
dates.append(datetime.datetime.now())
```

Let's load them into Pandas so we can analyze them:

In [3]:

```
import pandas as pd
date_series = pd.Series(dates)
```

In [4]:

```
date_series.head()
```

Out[4]:

In [5]:

```
date_series.describe()
```

Out[5]:

Let's remove all duplicates:

In [6]:

```
uniq_date_series = date_series.drop_duplicates()
uniq_date_series.describe()
```

Out[6]:

Now let's figure out the delta between all uniqe dates:

In [7]:

```
deltas = uniq_date_series - uniq_date_series.shift(1)
deltas.describe()
```

Out[7]:

And the smallest delta is

In [8]:

```
deltas.min()
```

Out[8]:

This means that the smallest increment between two consecutive dates is **1ms**. So we can not use the datetime on Windows to measure events that occur more frequently than 1ms.

And that is in the best case. This number will vary depending on how many other processes are running and what the Python code does in between two measurements.

Site proudly generated by Hakyll.