Reputation: 10861
This is really fast:
1 in range(100000000000000)
This is really slow:
1.5 in range(100000000000000)
Why does the full range have to be generated to know that 1.5
isn't in range(X)
when step
has to be an integer?
Upvotes: 3
Views: 78
Reputation: 12347
If we check the source code:
The contains function:
range_contains(rangeobject *r, PyObject *ob)
{
if (PyLong_CheckExact(ob) || PyBool_Check(ob))
return range_contains_long(r, ob);
return (int)_PySequence_IterSearch((PyObject*)r, ob,
PY_ITERSEARCH_CONTAINS);
}
It appears to check if it will use an integer or boolean method to check, and if not it uses the PY_ITERSEARCH_CONTAINS.
Upvotes: 3