Micro-optimization for list_contains. Factored double if test

out of the loop.
This commit is contained in:
Raymond Hettinger 2002-09-05 14:23:49 +00:00
parent 63857a454d
commit aae5999b44
1 changed files with 7 additions and 8 deletions

View File

@ -320,16 +320,15 @@ list_length(PyListObject *a)
static int
list_contains(PyListObject *a, PyObject *el)
{
int i;
int i, cmp;
for (i = 0; i < a->ob_size; ++i) {
int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Py_EQ);
if (cmp > 0)
return 1;
else if (cmp < 0)
return -1;
}
if (cmp > 0)
return 1;
if (cmp < 0)
return -1;
return 0;
}