Skip to content

perf: avoid floating-point formatting in microtime - #23604

Open
henderkes wants to merge 1 commit into
php:masterfrom
henderkes:perf/microtime-cast
Open

perf: avoid floating-point formatting in microtime#23604
henderkes wants to merge 1 commit into
php:masterfrom
henderkes:perf/microtime-cast

Conversation

@henderkes

Copy link
Copy Markdown
Contributor

Convert to long instead of double and save the division. Extra gain because zend_strpprintf() takes the cheaper integer formatting path instead of having to call zend_dtoa().

This isn't a bug fix so I guess it should target master? @devnexen

@TimWolla

TimWolla commented Sep 7, 2026

Copy link
Copy Markdown
Member

This isn't a bug fix so I guess it should target master?

Yes.

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering if microtime() without true is something that actually happens in the real world, but looks correct and simple enough to me.

@henderkes

henderkes commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Yes, it saved half a second in phpbench. Though I guess arguable whether that's real-world usage. Stumbled upon this randomly when tracing back codegen differences between gcc tailcall (patch open) and clang tailcall.

Edit: sorry, @staabm, I meant phpbench. Not phpstan.

@staabm

staabm commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

do these lines have the same problem?

ext/standard/uniqid.c:72-79 also uses %.8F for a generated seed string.
sapi/phpdbg/phpdbg_out.c:101-105 uses %.8F in debug logging output.

@henderkes

Copy link
Copy Markdown
Contributor Author

Yes, but I'll create a new PR since this one was already approved.

@staabm

staabm commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

to re-iterate this and hopefully I got this right:

microtime(true) is 10x faster than microtime(), right?
https://gist.github.com/staabm/ae867df5f19f88c56cd4de4418763fd3
(before this PR)

hrtime(true) and hrtime() are equal fast though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants