Bernie,
I think we still not comparing apples to apples. I posted the code which does few things programmatically:
1. drop collection
2. create collection
3. create index
4. inject data
5. look-up and count the data
I did what you posted though, i.e. measure with timeit the results from find().count() and they looks a like with your observations.
What I posted is slightly different. The code creates an index and inject the data and THAT causes first call to find().count() to delay with the rest. What I see/suspect is that insert
code does delay the flush of the data by the time I invoke find().count() and this behavior is different in different version of pymongo.
When you say you can't reproduce it, does it literally mean that you run posted code or you run timeit as you show?
If later, then we don't compare apples to apples. I really interested to know if you can reproduce behavior with posted code. The code is trivial and suppose to work to perform identical in different pymongo versions.
Moreover, I made another observation. If I'll not use insert_many and only use insert in a while loop and run both pymongo version than
I do see consistent results:
pymongo 3.2.2, ndocs 19998, time 0.022824048996
pymongo 3.2.2, ndocs 19998, time 0.0035879611969
pymongo 2.6.3, ndocs 29997, time 0.0308830738068
pymongo 2.6.3, ndocs 29997, time 0.005450963974
so the problem is "hidden" in a way insert_many operates under the hood and it cause an issue if someone call find().count() write after it.
Best,
Valentin.