Skip to content

Commit 78af087

Browse files
committed
PERF: Simplify some of loadtxt's standard converters.
Standard converters only ever get called with str inputs (loadtxt performs the required decoding); saving a bunch of runtime typechecks (in `asstr`) results in a 15-20% speedup when loadtxt()ing the corresponding types.
1 parent c42f57b commit 78af087

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

numpy/lib/npyio.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,9 @@ def _floatconv(x):
747747
(np.integer, lambda x: int(float(x))),
748748
(np.longdouble, np.longdouble),
749749
(np.floating, _floatconv),
750-
(complex, lambda x: complex(asstr(x).replace('+-', '-'))),
751-
(np.bytes_, asbytes),
752-
(np.unicode_, asunicode),
750+
(complex, lambda x: complex(x.replace('+-', '-'))),
751+
(np.bytes_, methodcaller('encode', 'latin-1')),
752+
(np.unicode_, str),
753753
]
754754

755755

@@ -763,7 +763,7 @@ def _getconv(dtype):
763763
for base, conv in _CONVERTERS:
764764
if issubclass(dtype.type, base):
765765
return conv
766-
return asstr
766+
return str
767767

768768

769769
# _loadtxt_flatten_dtype_internal and _loadtxt_pack_items are loadtxt helpers

0 commit comments

Comments
 (0)