Skip to content

non_ascii_to_octal: Return the input string if it only contains printable ASCII characters - #3199

Merged
seisman merged 1 commit into
mainfrom
non_ascii_to_octal/faster
Apr 25, 2024
Merged

non_ascii_to_octal: Return the input string if it only contains printable ASCII characters#3199
seisman merged 1 commit into
mainfrom
non_ascii_to_octal/faster

Conversation

@seisman

Copy link
Copy Markdown
Member

Description of proposed changes

The non_ascii_to_octal function was added in #2584 to convert any non-ASCII characters in a string to the corresponding octal codes. It's applied to all argument strings and texts passed in Figure.text. In most cases, we don't use non-ASCII characters, so applying this function is not necessary.

This PR improve the non_ascii_to_octal function so that it simply return the input string if the input string doesn't contain any non-ASCII characters.

Here is the benchmark.

Main branch

In [1]: frompygmt.helpersimportnon_ascii_to_octalIn [2]: argstr="".join([chr(c) forcinrange(32, 127)])
In [3]: argstrOut[3]: ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'In [4]: %timeitnon_ascii_to_octal(argstr)
243µs ± 1.08µsperloop (mean ± std. dev. of7runs, 1,000loopseach)
In [5]: %timeitnon_ascii_to_octal(argstr+"α")
244µs ± 1.03µsperloop (mean ± std. dev. of7runs, 1,000loopseach)

This PR

In [1]: frompygmt.helpersimportnon_ascii_to_octalIn [2]: argstr="".join([chr(c) forcinrange(32, 127)])
In [3]: argstrOut[3]: ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'In [4]: %timeitnon_ascii_to_octal(argstr)
5.82µs ± 8.87nsperloop (mean ± std. dev. of7runs, 100,000loopseach)
In [5]: %timeitnon_ascii_to_octal(argstr+"α")
240µs ± 233nsperloop (mean ± std. dev. of7runs, 1,000loopseach)

For strings that don't contain any non-ASCII characters, this PR is much faster (5.8 µs vs 243 µs).

@seismanseisman added the enhancement Improving an existing feature label Apr 24, 2024
@seismanseisman added this to the 0.12.0 milestone Apr 24, 2024
@seismanseisman added the run/benchmark Trigger the benchmark workflow in PRs label Apr 24, 2024
@codspeed-hq

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #3199 will improve performances by 82.47%

Comparing non_ascii_to_octal/faster (1eca4e4) with main (99bc5d3)

Summary

⚡ 43 improvements
✅ 56 untouched benchmarks

Benchmarks breakdown

Benchmarkmainnon_ascii_to_octal/fasterChange
test_basemap91.5 ms72.3 ms+26.65%
test_binstats_no_outgrid166.2 ms128 ms+29.8%
test_colorbar88.1 ms74.7 ms+17.99%
test_config_format_date_map266.4 ms239.2 ms+11.35%
test_dimfilter_no_outgrid79.8 ms46.3 ms+72.14%
test_figure_repr251 ms176.9 ms+41.91%
test_grd2cpt237.2 ms210.6 ms+12.67%
test_grdclip_no_outgrid72.5 ms45.5 ms+59.27%
test_grdcontour_labels133.1 ms92.5 ms+43.86%
test_grdcut_dataarray_in_dataarray_out94.6 ms74 ms+27.85%
test_grdfill_dataarray_out59.3 ms45.8 ms+29.41%
test_grdfilter_dataarray_in_dataarray_out79.7 ms46.2 ms+72.63%
test_grdgradient_no_outgrid65.9 ms45.8 ms+44.1%
test_compute_bins_no_outfile63.9 ms43.7 ms+46.35%
test_equalize_grid_no_outgrid65.6 ms45.4 ms+44.31%
test_grdinfo41.7 ms28.3 ms+47.51%
test_grdproject_no_outgrid[+proj=merc +ellps=WGS84 +units=m +width=10]82.5 ms55.7 ms+48.21%
test_grdproject_no_outgrid[EPSG:3395 +width=10]75.6 ms48.8 ms+54.87%
test_grdproject_no_outgrid[M10c]72.6 ms45.8 ms+58.42%
test_grdsample_dataarray_out78.6 ms54.6 ms+44%
...............

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@seismanseisman added needs review This PR has higher priority and needs review. and removed run/benchmark Trigger the benchmark workflow in PRs labels Apr 24, 2024
@seismanseisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Apr 24, 2024
@seisman
seisman merged commit bc673bc into mainApr 25, 2024
@seisman
seisman deleted the non_ascii_to_octal/faster branch April 25, 2024 00:21
@seismanseisman removed the final review call This PR requires final review and approval from a second reviewer label Apr 25, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementImproving an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@seisman@michaelgrund