Skip to content

Commit b698772

Browse files
committed
Use explicit len dispatch to avoid overhead
1 parent 077d353 commit b698772

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

pandas/lib.pyx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
1818
PyBytes_Check,
1919
PyTuple_SetItem,
2020
PyTuple_New,
21-
PyObject_SetAttrString)
21+
PyObject_SetAttrString,
22+
PyString_GET_SIZE,
23+
PyBytes_GET_SIZE,
24+
PyUnicode_GET_SIZE)
2225

2326
cdef extern from "Python.h":
2427
Py_ssize_t PY_SSIZE_T_MAX
@@ -901,18 +904,20 @@ def clean_index_list(list obj):
901904
def max_len_string_array(ndarray arr):
902905
""" return the maximum size of elements in a 1-dim string array """
903906
cdef:
904-
int i, m, l
905-
int length = arr.shape[0]
907+
Py_ssize_t i, m = 0, l = 0, length = arr.shape[0]
906908
object v
907909

908-
m = 0
909-
for i from 0 <= i < length:
910+
for i in range(length):
910911
v = arr[i]
911-
if PyString_Check(v) or PyBytes_Check(v) or PyUnicode_Check(v):
912-
l = len(v)
913-
914-
if l > m:
915-
m = l
912+
if PyString_Check(v):
913+
l = PyString_GET_SIZE(v)
914+
elif PyBytes_Check(v):
915+
l = PyBytes_GET_SIZE(v)
916+
elif PyUnicode_Check(v):
917+
l = PyUnicode_GET_SIZE(v)
918+
919+
if l > m:
920+
m = l
916921

917922
return m
918923

0 commit comments

Comments
 (0)