3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); // Make a bit, honkin test array // You may need to adjust this depth to avoid memory limit errors $testArray = [ 'string' => 'string', 'int' => 1, 'float' => 1.1, 'bool' => true, 'date' => new Datetime('now'), 'array' => [ 'string' => 'string', 'int' => 1, 'float' => 1.1, 'bool' => true, 'date' => new Datetime('now'), 'array' => [ 'string' => 'string', 'int' => 1, 'float' => 1.1, 'bool' => true, 'date' => new Datetime('now'), ], ], fillArray(0, 5) ]; $numRuns = 10000000; // Time json encoding $start = microtime( true ); for ($i = 0; $i++; $i <= $numRuns) { json_encode( $testArray ); } $jsonTime = (microtime( true ) - $start); echo "JSON encoded in " . ($jsonTime/$numRuns) . " seconds\n"; // Time serialization $start = microtime( true ); for ($i = 0; $i++; $i <= $numRuns) { serialize( $testArray ); } $serializeTime = (microtime( true ) - $start); echo "PHP serialized in " . ($serializeTime/$numRuns) . " seconds\n"; // Compare them if ( $jsonTime < $serializeTime ) { echo "json_encode() was roughly " . number_format( ($serializeTime / $jsonTime - 1 ) * 100, 2 ) . "% faster than serialize()"; } else if ( $serializeTime < $jsonTime ) { echo "serialize() was roughly " . ($serializeTime ? number_format( ($jsonTime / $serializeTime - 1) * 100, 2 ) . '%' : 'insanely') . " faster than json_encode()"; } else { echo 'This is a draw'; } function fillArray( $depth, $max ) { static $seed; if ( is_null( $seed ) ) { $seed = array( 'a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10 ); } if ( $depth < $max ) { $node = array(); foreach ( $seed as $key ) { $node[$key] = fillArray( $depth + 1, $max ); } return $node; } return 'empty'; }
Output for 8.1.2 - 8.1.4, 8.1.6, 8.1.9 - 8.1.10, 8.1.14, 8.1.16 - 8.1.17, 8.1.21, 8.1.23 - 8.1.24, 8.1.27 - 8.1.28, 8.1.30 - 8.1.31, 8.2.2 - 8.2.3, 8.2.5, 8.2.7, 8.2.9, 8.2.12, 8.2.14, 8.2.16 - 8.2.18, 8.2.24, 8.3.3, 8.3.5 - 8.3.6, 8.3.13, 8.4.1 - 8.4.2
JSON encoded in 9.5367431640625E-14 seconds PHP serialized in 0 seconds serialize() was roughly insanely faster than json_encode()
Output for 8.1.11 - 8.1.12, 8.2.0, 8.2.6, 8.2.20, 8.2.25, 8.3.19
JSON encoded in 9.5367431640625E-14 seconds PHP serialized in 9.5367431640625E-14 seconds This is a draw
Output for 8.2.21, 8.3.12, 8.3.15
JSON encoded in 9.5367431640625E-14 seconds PHP serialized in 1.1920928955078E-13 seconds json_encode() was roughly 25.00% faster than serialize()
Output for 8.3.14
JSON encoded in 1.1920928955078E-13 seconds PHP serialized in 1.1920928955078E-13 seconds This is a draw
Output for 8.3.8, 8.3.11
JSON encoded in 1.9073486328125E-13 seconds PHP serialized in 0 seconds serialize() was roughly insanely faster than json_encode()
Output for 8.1.13, 8.2.11, 8.2.26, 8.3.0 - 8.3.1, 8.3.10
JSON encoded in 1.1920928955078E-13 seconds PHP serialized in 0 seconds serialize() was roughly insanely faster than json_encode()
Output for 8.3.9
JSON encoded in 2.1457672119141E-13 seconds PHP serialized in 9.5367431640625E-14 seconds serialize() was roughly 125.00% faster than json_encode()
Output for 8.1.7, 8.1.15, 8.1.22, 8.2.8, 8.3.7
JSON encoded in 1.1920928955078E-13 seconds PHP serialized in 9.5367431640625E-14 seconds serialize() was roughly 25.00% faster than json_encode()
Output for 8.1.0 - 8.1.1, 8.1.5, 8.1.8, 8.1.20, 8.1.25 - 8.1.26, 8.2.1, 8.2.10, 8.2.13, 8.2.19, 8.2.22 - 8.2.23, 8.2.27, 8.3.2, 8.3.4
JSON encoded in 0 seconds PHP serialized in 0 seconds This is a draw
Output for 8.2.15
JSON encoded in 2.1457672119141E-13 seconds PHP serialized in 0 seconds serialize() was roughly insanely faster than json_encode()
Output for 8.2.4
JSON encoded in 1.9073486328125E-13 seconds PHP serialized in 1.1920928955078E-13 seconds serialize() was roughly 60.00% faster than json_encode()
Output for 8.1.18 - 8.1.19, 8.1.29
JSON encoded in 0 seconds PHP serialized in 9.5367431640625E-14 seconds Fatal error: Uncaught DivisionByZeroError: Division by zero in /in/EX86b:53 Stack trace: #0 {main} thrown in /in/EX86b on line 53
Process exited with code 255.

preferences:
49.62 ms | 422 KiB | 5 Q