Skip to content

Zend: Improve integer to string conversion performance - #25444

Open
adapik wants to merge 1 commit into
php:masterfrom
adapik:int-to-string-two-digits
Open

adapik wants to merge 1 commit into
php:masterfrom
adapik:int-to-string-two-digits

Conversation

@adapik

@adapik adapik commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Instead of one digit per iteration (% 10, / 10), print two digits at a time from a 200-byte "00".."99" table, and divide by 10000 per loop step. Output is unchanged.

It's not a new idea: most of the other programming languages do this trick (Rust, Go, Java, etc.).

Numbers up to ~3 digits don't change much, but longer ones get noticeably faster:

workload 4-8 digits 15-19 digits
implode() -16..-20% -20..-24%
json_encode() -9..-16% -31..-37%
serialize() -7..-12% -22..-30%
(string)$i / echo -3..-14% -17..-25%

The C function alone is ~1.8x faster.

Comment thread Zend/zend_operators.h Resolved
Comment thread Zend/zend_operators.c Resolved
Comment thread Zend/zend_operators.h Resolved
@adapik
adapik force-pushed the int-to-string-two-digits branch 2 times, most recently from d98534c to 5fb539d Compare November 14, 2026 09:52
@adapik
adapik requested a review from iliaal November 14, 2026 10:03
Comment thread Zend/zend_operators.c Resolved
@adapik
adapik force-pushed the int-to-string-two-digits branch from 5fb539d to 3110786 Compare November 14, 2026 15:57
@adapik
adapik force-pushed the int-to-string-two-digits branch from 3110786 to f0b1170 Compare November 14, 2026 16:06
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.

2 participants