1
0
mirror of https://github.com/php/php-src.git synced 2026-04-28 02:33:17 +02:00
Files
archived-php-src/ext
Tim Düsterhus 18d99ee4b7 json: Improve performance of php_json_encode_array() (#20092)
Instead of using a boolean flag to check for each element whether or not it is
the first, we just unconditionally append a comma after each element and then
remove the last comma at the end.

For:

    <?php

    $len = 0;
    for ($i = 0; $i < 3_000_000; $i++) {
    	$len += strlen(json_encode(array_fill(0, 20, [])));
    }
    var_dump($len);

This is ~1.06 faster for a gcc 13.3 release build on a Intel(R) Core(TM)
i7-1365U.

    Benchmark 1: /tmp/bench/before /tmp/bench/test6.php
      Time (mean ± σ):     819.6 ms ±   2.8 ms    [User: 816.4 ms, System: 2.4 ms]
      Range (min … max):   816.9 ms … 825.0 ms    10 runs

    Benchmark 2: /tmp/bench/after /tmp/bench/test6.php
      Time (mean ± σ):     770.8 ms ±   5.8 ms    [User: 766.6 ms, System: 2.9 ms]
      Range (min … max):   765.3 ms … 785.8 ms    10 runs

    Summary
      /tmp/bench/after /tmp/bench/test6.php ran
        1.06 ± 0.01 times faster than /tmp/bench/before /tmp/bench/test6.php
2025-10-08 10:10:16 +02:00
..
2025-09-22 15:43:47 +02:00
2025-10-07 14:43:56 +02:00
2025-10-06 10:32:28 +02:00
2025-10-06 10:32:28 +02:00
2025-09-29 23:21:08 +01:00
2025-09-30 15:21:19 +02:00
2025-09-29 12:45:45 -03:00
2025-10-07 10:54:05 +02:00
2025-07-29 14:00:37 +02:00
2025-10-06 10:32:28 +02:00
2025-10-06 10:32:28 +02:00
2025-09-29 16:09:07 +02:00
2025-10-06 10:32:28 +02:00
2025-10-06 21:51:18 +02:00
2025-09-29 22:49:12 +01:00