Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Micro optimization of plotting#26303

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
oscargus merged 9 commits intomatplotlib:mainfromeendebakpt:plotting_optimizations
Jul 16, 2023

Conversation

@eendebakpt
Copy link
Contributor

PR summary

In this PR some micro optimizations are applied to the matplotlib plotting. Optimizations selected are in methods that show in the profiling (cProfile).

Benchmark

Mean +- std dev: [main] 2.58 ms +- 0.23 ms -> [pr] 2.49 ms +- 0.23 ms: 1.04x faster

on script

import matplotlib# print(matplotlib)import pyperfsetup = """import matplotlib.pyplot as pltx=[1,2,3,4,5]y=[5,6,3,3,4]n=6def go():    for ii in range(n):        plt.figure(1)        plt.plot(x,y)"""runner = pyperf.Runner()runner.timeit(name="mpl", stmt="go()", setup=setup)

PR checklist

@tacaswell
Copy link
Member

importmatplotlib# print(matplotlib)importpyperfsetup="""import matplotlib.figure as mfigurex=[1,2,3,4,5]y=[5,6,3,3,4]n=6def go():    for ii in range(n):        fig = mfigure.Figure()        ax = fig.subplots()        ax.plot(x,y)"""runner=pyperf.Runner()runner.timeit(name="mpl",stmt="go()",setup=setup)

This may be a better bench mark script.

I have concerns that the speed up is less than the std....

@tacaswell
Copy link
Member

well, did you mean to plot many times to the same figure or do a plot per figure?

@tacaswell
Copy link
Member

new (this branch)

mpl: Mean +- std dev: 796 us +- 7 us

old (3.7.2 from wheels)

mpl: Mean +- std dev: 845 us +- 13 us

using

importmatplotlib# print(matplotlib)importpyperfsetup="""import matplotlib.figure as mfigurex=[1,2,3,4,5]y=[5,6,3,3,4]n=6fig = mfigure.Figure()ax = fig.subplots()def go():    for ii in range(n):        ax.plot(x,y)"""runner=pyperf.Runner()runner.timeit(name="mpl",stmt="go()",setup=setup)

so I think there is a real speed up here, even if it is realitvely small against the cost of making a newFigure andAxes (that is probably why the std was so high...the first run took an order of magnitude longer!)

@tacaswelltacaswell added this to thev3.8.0 milestoneJul 13, 2023
@tacaswelltacaswell requested a review fromoscargusJuly 13, 2023 20:29
Copy link
Member

@oscargusoscargus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Most things make sense for sure. Some things I simply trust are faster.

A few minor comments though.

@eendebakpteendebakpt changed the titleDraft: micro optimization of plottingMicro optimization of plottingJul 14, 2023
defsafe_isfinite(val):
ifvalisNone:
returnFalse
ifisinstance(val,int):
Copy link
Contributor

@anntzeranntzerJul 15, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure special-casing ints here make sense -- certainly that'll make a microbenchmark based on plotting ints faster, but at the cost of introducing a branch for all other cases (plotting floats is likely much more common, and even np.int is perhaps a more common case than python ints in real code).

If one really wants to workaround the fact that np.isfinite is relatively slow, one can instead use math.isfinite (which on a quick microbenchmark is extremely fast), taking into account the fact that it won't handle certain cases like datetimes (but will handle numpy floats and ints), so something like

try:ifmath.isfinite(val):returnTrueexceptTypeError:pass# continue with the np.isfinite check

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is an excellent suggestion. On my systemnp.isfinite(val) if np.isscalar(val) else True is about 900 ns, andmath.isfinite(val) (including the try-except) is 80 ns. Theisinstance(val, int) is 50 ns, but does not handle the important float case.

@oscargusoscargus merged commitb0121b6 intomatplotlib:mainJul 16, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@tacaswelltacaswelltacaswell approved these changes

@anntzeranntzeranntzer left review comments

@oscargusoscargusoscargus approved these changes

+1 more reviewer

@tfpftfpftfpf left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

v3.8.0

Development

Successfully merging this pull request may close these issues.

6 participants

@eendebakpt@tacaswell@anntzer@oscargus@tfpf@melissawm

[8]ページ先頭

©2009-2025 Movatter.jp