Skip to content

Optimize getFormattedTime method with snprintf#87

Merged
techniccontroller merged 1 commit into
techniccontroller:mainfrom
deepstahl77:patch-1
Jul 11, 2026
Merged

Optimize getFormattedTime method with snprintf#87
techniccontroller merged 1 commit into
techniccontroller:mainfrom
deepstahl77:patch-1

Conversation

@deepstahl77

@deepstahl77 deepstahl77 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Refactor time formatting to use snprintf for efficiency and avoid heap fragmentation.

The original builds 4 separate String objects (hoursStr, minuteStr, secondStr + the final concatenation), my version builds only a single String at the very end – everything else happens in the fixed char buffer on the stack, with no heap allocations. Output format (e.g. "09:05:03") is identical. no need to change anything in ntp_client_plus.h

Refactor time formatting to use snprintf for efficiency and avoid heap fragmentation.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors NTPClientPlus::getFormattedTime() to format hh:mm:ss using a fixed-size stack buffer and snprintf, reducing intermediate String allocations (and therefore reducing heap churn) while keeping the output identical.

Changes:

  • Replaced per-component String creation + concatenation with snprintf into a char[9] buffer.
  • Constructed the final String once from the formatted buffer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@techniccontroller techniccontroller merged commit c40ed9a into techniccontroller:main Jul 11, 2026
1 check passed
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.

3 participants