From 4a7920c6e5e21e4fefa228366f6e313aba77818a Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 22 Jun 2022 11:40:38 +0200 Subject: [PATCH 1/3] improve performance of is_lexsorted --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/_libs/algos.pyx | 2 ++ 2 files changed, 3 insertions(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 76f6e864a174f..373323649eacb 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -739,6 +739,7 @@ Performance improvements - Performance improvement in :func:`factorize` (:issue:`46109`) - Performance improvement in :class:`DataFrame` and :class:`Series` constructors for extension dtype scalars (:issue:`45854`) - Performance improvement in :func:`read_excel` when ``nrows`` argument provided (:issue:`32727`) +- Performance improvement in :meth:`MultiIndex.is_monotonic_increasing` (:issue:`47458`) .. --------------------------------------------------------------------------- .. _whatsnew_150.bug_fixes: diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index d33eba06988e9..29f9a22c9b36e 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -180,6 +180,8 @@ def is_lexsorted(list_of_arrays: list) -> bint: else: result = False break + if not result: + break free(vecs) return result From 8976351bd96abdb2e8de9867a07695a3764891e6 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Thu, 23 Jun 2022 16:55:05 +0200 Subject: [PATCH 2/3] early return --- pandas/_libs/algos.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 29f9a22c9b36e..4e52f8bc46b86 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -156,7 +156,6 @@ def is_lexsorted(list_of_arrays: list) -> bint: Py_ssize_t n, nlevels int64_t k, cur, pre ndarray arr - bint result = True nlevels = len(list_of_arrays) n = len(list_of_arrays[0]) @@ -181,9 +180,10 @@ def is_lexsorted(list_of_arrays: list) -> bint: result = False break if not result: - break + free(vecs) + return False free(vecs) - return result + return True @cython.boundscheck(False) From 01d2d18fc54790d38987afaa1a86fb478f8f8560 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Thu, 23 Jun 2022 17:10:41 +0200 Subject: [PATCH 3/3] undo early return --- pandas/_libs/algos.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 4e52f8bc46b86..29f9a22c9b36e 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -156,6 +156,7 @@ def is_lexsorted(list_of_arrays: list) -> bint: Py_ssize_t n, nlevels int64_t k, cur, pre ndarray arr + bint result = True nlevels = len(list_of_arrays) n = len(list_of_arrays[0]) @@ -180,10 +181,9 @@ def is_lexsorted(list_of_arrays: list) -> bint: result = False break if not result: - free(vecs) - return False + break free(vecs) - return True + return result @cython.boundscheck(False)