Skip to content

Optimize Study.best_trial performance by avoiding unnecessary deep copy#6119

Merged
toshihikoyanase merged 12 commits intooptuna:masterfrom
msdsm:feature/update-study-best-trials
Jun 3, 2025
Merged

Optimize Study.best_trial performance by avoiding unnecessary deep copy#6119
toshihikoyanase merged 12 commits intooptuna:masterfrom
msdsm:feature/update-study-best-trials

Conversation

@msdsm
Copy link
Copy Markdown
Contributor

@msdsm msdsm commented May 31, 2025

Motivation

Description of the changes

  • Added private method _get_best_trial(deepcopy: bool = True)
  • Modified best_trial property to call _get_best_trial(deepcopy=True)
  • Updated _log_completed_trial to use _get_best_trial(deepcopy=False)

Performance

Example code execution time improved from 1.5650s → 1.2588s

import optuna
def objective(trial):
    x = trial.suggest_float("x", -1, 1)
    y = trial.suggest_int("y", -1, 1)
    return x**2 + y

study = optuna.create_study(sampler=optuna.samplers.RandomSampler())
study.optimize(objective, n_trials=10000)

Loading
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. sprint-20250531 PR from the online sprint event May 31, 2025.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants