diff options
author | Raymond Hettinger <python@rcn.com> | 2002-09-05 14:23:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2002-09-05 14:23:49 (GMT) |
commit | aae5999b44e54394f91e84b775d29230d3a0a2ef (patch) | |
tree | b9310059bcb3f93fe8988198a83a1eacd79bef99 /Objects/listobject.c | |
parent | 63857a454d85fda475264575b7c1a031970219a0 (diff) | |
download | cpython-aae5999b44e54394f91e84b775d29230d3a0a2ef.zip cpython-aae5999b44e54394f91e84b775d29230d3a0a2ef.tar.gz cpython-aae5999b44e54394f91e84b775d29230d3a0a2ef.tar.bz2 |
Micro-optimization for list_contains. Factored double if test
out of the loop.
Diffstat (limited to 'Objects/listobject.c')
-rw-r--r-- | Objects/listobject.c | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index cea8597..229d64c 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -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; } |