Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace inspect.stack() with traceback.extract_stack() to solve performance bottleneck #716

Merged
merged 1 commit into from
Aug 6, 2023

Conversation

aploium
Copy link
Contributor

@aploium aploium commented Jun 25, 2023

Use traceback.extract_stack() instead of inspect.stack() to check upper stack method, they are at same functionality, but the traceback(5μs) is 400x faster than inspect.stack(2ms). This method affects many common operation, such as add() and from_timestamp(tz), 2ms delay typically cause performance issues.
This method was available since early py 2.x

EDIT: the performance diff is not 50x, but 400x

Performance

before

%%timeit
inspect.stack()[1][3]
# 2.11 ms ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
pendulum.from_timestamp(1687658855, "Asia/Shanghai")
# 2.21 ms ± 63 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

after

%%timeit
traceback.extract_stack(limit=2)[0].name
# 3.7 µs ± 73.5 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

%%timeit
pendulum.from_timestamp(1687658855, "Asia/Shanghai")
# 8.45 µs ± 142 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Calling Result Demo

import inspect
import traceback

def call1(f):
    f()
def call2(f):
    call1(f)

def old():
    print("old return is->", inspect.stack()[1][3])
def new():
    print("new return is->", traceback.extract_stack(limit=2)[0].name)


call2(old)
call2(new)

The output is

old return is-> call1
new return is-> call1

Pull Request Check List

  • [ x ] Added tests for changed code.
    No new tests required
  • [ x ] Updated documentation for changed code.
    No new document required

…er stack method, they are at same functionality, but the traceback(5μs) is 50x faster than inspect.stack(2ms). This method affects many common operation, such as add() and from_timestamp(tz), 2ms delay typically cause performance issues.
@sdispater sdispater merged commit 68e30cd into python-pendulum:master Aug 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants