Skip to content

Remove unnecessary NumPy usage#6215

Merged
y0z merged 2 commits intooptuna:masterfrom
nabenabe0928:enhance/use-math-instead-of-numpy
Jul 31, 2025
Merged

Remove unnecessary NumPy usage#6215
y0z merged 2 commits intooptuna:masterfrom
nabenabe0928:enhance/use-math-instead-of-numpy

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 commented Jul 24, 2025

Motivation

math is usually quicker than numpy for a scalar, so I replaced numpy with math.

Description of the changes

  • Use math instead of numpy for a scalar call

Note

math.ceil returns an integer value instead of a floating number. ref

Benchmarking

In [1]: import math
   ...: import numpy as np
   ...: 
   ...: 
   ...: %timeit math.isinf(100)
   ...: %timeit np.isinf(100)    
16 ns ± 0.776 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)
390 ns ± 9.09 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [2]: %timeit 1000**0.5
   ...: %timeit math.sqrt(1000)
   ...: %timeit np.sqrt(1000)
3.37 ns ± 0.0315 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)
21.1 ns ± 0.662 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)
573 ns ± 1.84 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [3]: %timeit math.ceil(0.1 * 777)
   ...: %timeit np.ceil(0.1 * 777)
14.6 ns ± 0.269 ns per loop (mean ± std. dev. of 7 runs, 100,000,000 loops each)
394 ns ± 2.01 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

@nabenabe0928 nabenabe0928 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Jul 24, 2025
@y0z y0z self-assigned this Jul 24, 2025
@y0z
Copy link
Copy Markdown
Member

y0z commented Jul 24, 2025

@sawa3030
Could you review this PR?

Co-authored-by: Eri Sawada <125344906+sawa3030@users.noreply.github.com>
Copy link
Copy Markdown
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

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

LGTM

@y0z y0z removed their assignment Jul 25, 2025
@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 30, 2025
Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 left a comment

Choose a reason for hiding this comment

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

LGTM

@y0z y0z merged commit 722da91 into optuna:master Jul 31, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants