3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * This is a perfomance test suite */ require '_functions.php'; set_time_limit(600); ########################################################### # functions ########################################################### function start( $first, $second, $description, $iteration ) { echo ' ###############################################<br/> <strong>'.$first.' vs. '.$second.': ['.$iteration.']</strong><br/>'; if( !empty($description) ) { echo '<i>'.$description.'</i><br/>'; } // if } function result( $first, $second, $iteration ) { echo '<strong>Time/Call: '.round($first/$iteration*1000000, 2).'┬Ásec / '.round($second/$iteration*1000000, 2).'┬Ásec<br/>'; echo 'Speedup: '.round(($first/$second)*100).'% ['.round($first/$second, 1).'x faster]</strong><br/><br/><br/>'; flush(); } ########################################################### # guinea pigs ########################################################### date_default_timezone_set( 'UTC' ); $dateTimeNow = new DateTime(); // text with 10 word and 68 chars in 1 lines $shortString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus jus'; // text with 95 word and 681 chars in 3 lines $mediumString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus justo. Morbi molestie augue non nisl. Sed tincidunt scelerisque tellus. Praesent blandit arcu ac sem. Curabitur commodo, turpis in vehicula tristique, libero neque venenatis metus, nec fermentum justo libero ac ante. In hac habitasse platea dictumst. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Phasellus laoreet ultricies enim. Suspendisse potenti. Phasellus placerat semper nibh. Curabitur tincidunt. Nulla facilisi. Fusce quis nibh. In hac habitasse platea dictumst. Curabitur nunc magna, ultricies quis, mattis vel, pretium vitae, pede. Fusce faucibus. Ut quam pe'; // text with 1000 word and 6818 chars in 21 lines $longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus justo. Morbi molestie augue non nisl. Sed tincidunt scelerisque tellus. Praesent blandit arcu ac sem. Curabitur commodo, turpis in vehicula tristique, libero neque venenatis metus, nec fermentum justo libero ac ante. In hac habitasse platea dictumst. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Phasellus laoreet ultricies enim. Suspendisse potenti. Phasellus placerat semper nibh. Curabitur tincidunt. Nulla facilisi. Fusce quis nibh. In hac habitasse platea dictumst. Curabitur nunc magna, ultricies quis, mattis vel, pretium vitae, pede. Fusce faucibus. Ut quam pede, pulvinar eget, euismod eget, pellentesque non, tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas quis dolor. Vivamus ut nisl id orci ultrices blandit. Aliquam erat volutpat. Pellentesque felis. Nulla facilisi. Donec nulla erat, pharetra vel, commodo eget, tristique ac, pede. Etiam sagittis suscipit dui. Mauris imperdiet sodales diam. Etiam quis mi. Maecenas erat dolor, pharetra et, rhoncus vehicula, commodo eu, dui. Proin eu velit. Integer feugiat. Phasellus euismod. Nunc lobortis, mi id pellentesque tempus, augue velit rhoncus purus, non iaculis orci tortor ut est. Donec id nibh sit amet eros interdum dictum. Pellentesque metus ligula, ullamcorper sit amet, faucibus eu, laoreet non, nunc. Cras at turpis. Nam nulla dolor, ullamcorper eu, sodales non, pellentesque et, velit. In ornare augue eu est. In hac habitasse platea dictumst. Quisque bibendum augue sed sem. Mauris hendrerit augue nec libero. Nullam eu urna eget erat dignissim fringilla. Ut at ante at velit gravida eleifend. Etiam sit amet sem. Integer venenatis. Nulla facilisi. Vivamus dignissim. In hac habitasse platea dictumst. Proin commodo mauris id pede. Donec fringilla fermentum diam. Praesent vestibulum ante nec velit. Vestibulum placerat. Quisque ornare, pede at molestie condimentum, turpis nulla vehicula leo, sed sodales tellus est feugiat lectus. In vitae nulla. Nam gravida metus eget leo. Nullam sapien arcu, fermentum quis, vulputate id, auctor eget, ligula. Curabitur tincidunt, neque in semper laoreet, metus elit congue eros, ac cursus nibh justo id sapien. Phasellus massa nisi, pulvinar sed, rutrum lacinia, ultrices a, est. Praesent ac tortor. Quisque semper. Integer elementum vestibulum erat. Maecenas tincidunt justo id arcu. Vivamus laoreet arcu. Ut dignissim felis vitae elit porttitor eleifend. Nunc neque. Proin eu turpis sit amet erat tincidunt feugiat. Sed arcu. In luctus. Vivamus rhoncus accumsan mauris. Nullam nisi pede, mollis congue, blandit ut, interdum sit amet, diam. Pellentesque in orci in tortor auctor volutpat. Aliquam quis tortor. Integer erat diam, congue a, aliquet at, blandit sed, mauris. Nullam aliquam. Maecenas nulla odio, mollis id, bibendum sed, laoreet eu, risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque neque quam, sagittis sit amet, ultricies quis, volutpat sit amet, quam. Donec vel tellus eget turpis posuere tincidunt. Duis ligula metus, egestas vel, aliquet id, pharetra eu, leo. Suspendisse nisl nunc, hendrerit eget, tempor ut, rhoncus a, risus. Donec sed massa. Nunc erat diam, facilisis quis, elementum sed, porttitor ut, sem. Proin rhoncus. Sed malesuada odio vitae ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed vitae leo non mi ornare lacinia. Curabitur massa risus, tincidunt vitae, elementum in, dignissim non, nibh. Cras euismod diam eu massa. Pellentesque augue risus, rutrum quis, posuere eu, tempus consequat, augue. Donec quis pede. Fusce blandit cursus turpis. Nam sit amet arcu. Sed eu ante quis quam imperdiet imperdiet. In hac habitasse platea dictumst. Morbi pellentesque. In hac habitasse platea dictumst. Vivamus sapien nisl, iaculis eu, rutrum id, tempor ac, nulla. Fusce feugiat est vitae sapien. Proin auctor. Pellentesque vitae magna eu risus mattis laoreet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer condimentum neque eu lorem. Nullam convallis, diam sed pretium aliquet, nisi mauris tempor orci, eu congue tortor velit nec quam. Etiam at justo at lectus tempus ultricies. Aenean vehicula mollis quam. Donec et elit. Donec egestas tellus et odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce ullamcorper aliquam ligula. Curabitur porttitor porttitor velit. Vestibulum ac turpis et nisi tincidunt scelerisque. Curabitur commodo. Sed dolor lacus, venenatis non, varius ac, commodo nec, orci. Fusce urna. Etiam euismod tincidunt risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse risus. Maecenas ut pede sit amet ante feugiat laoreet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis sit amet eros eget diam tincidunt malesuada. Morbi diam neque, lacinia vel, vulputate vitae, sodales ut, justo. Vestibulum risus. Duis libero. Aenean sit amet massa. In risus neque, tincidunt sit amet, volutpat sit amet, iaculis at, odio. Vestibulum arcu tellus, malesuada eget, aliquam sit amet, pharetra ac, purus. Sed a sapien. Maecenas sed nibh. Duis vitae purus. Sed congue fringilla tortor. Curabitur sit amet lorem. Donec cursus. Cras enim turpis, volutpat eu, vulputate ac, hendrerit quis, lectus. Etiam non risus. Cras pharetra dictum libero. Nulla venenatis. Nullam aliquet egestas lorem. Maecenas diam urna, convallis non, placerat vel, lobortis ut, magna. Sed interdum vestibulum quam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed magna elit, adipiscing vel, blandit vel, pharetra eu, ligula. Ut nisl. Nunc euismod imperdiet ipsum. Pellentesque et leo. Proin volutpat augue eu ante. Vestibulum mattis, velit at placerat iaculis, magna lorem luctus turpis, vitae interdum libero felis id nisi. Donec faucibus lorem nec diam. Nunc mollis ligula vitae tortor. Aenean ac sapien. Duis tristique urna at purus. Vestibulum porta, mi non pulvinar suscipit, tortor tellus faucibus dolor, gravida convallis urna libero quis tortor. Vestibulum dictum lacinia mauris. Sed pretium. Aenean ut lectus at mauris condimentum blandit. Sed mollis tempus lorem. Nulla ac lectus. Vestibulum diam turpis, rhoncus sed, interdum auctor, interdum eget, est. Vivamus eu nunc. Aenean cursus, nulla vel tristique iaculis, magna orci aliquet eros, vitae sodales nulla justo sed mi. Vivamus semper, sapien eget condimentum venenatis, felis quam scelerisque ipsum, sed posuere elit felis id sem. Mauris luctus mollis justo. Integer nec orci. Vestibulum at leo ut.'; $smallArray = array( 'a', 'b', 'c' ); $mediumArray = array( 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ); for( $i = 0; $i < 2600; ++$i ) { $largeArray[$i] = chr($i%26+97).'-'.$i; } // for ########################################################### # main ########################################################### htmlHeader(); /** * md5 vs. sha1 */ $iteration = 50000; start( 'MD5', 'SHA1', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $string = md5($mediumString); } $first = clock( 'md5' ); $i = $iteration; while( --$i ) { $string = sha1($mediumString); } $second = clock( 'sha1' ); result( $first, $second, $iteration ); /** * foreach with and without reference */ $iteration = 5000; start( 'foreach w/o reference', 'foreach w/ reference', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { foreach ($largeArray as $key => $value) { if ($value === '') { $largeArray[$key] = null; } } } $first = clock( 'foreach w/o reference' ); $i = $iteration; while( --$i ) { foreach ($largeArray as &$value) { if ($value === '') { $value = null; } } unset($value); } $second = clock( 'foreach w/ reference' ); result( $first, $second, $iteration ); /** * date() function vs. DateTime object */ $iteration = 100000; start( 'array_merge', '+', null, $iteration ); $flip = array_flip($mediumArray); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = array_merge( $mediumArray, $flip ); } $first = clock( 'array_merge' ); $i = $iteration; while( --$i ) { $result = $mediumArray + $flip; } $second = clock( '+' ); result( $first, $second, $iteration ); /** * date() function vs. DateTime object */ $iteration = 100000; start( 'dirname', 'basename', 'basename', $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = dirname( __FILE__ ); } $first = clock( 'dirname' ); $i = $iteration; while( --$i ) { $result = basename( __FILE__ ); } $second = clock( 'basename' ); result( $first, $second, $iteration ); /** * empty function call vs. small function call * * we try to show how expensive it is to make * any kind of function call */ $iteration = 1000000; start( 'empty function', 'small function', null, $iteration ); function __empty( $a, $b, $c ) { } function __small( $a, $b, $c ) { $a = $b + $c; } clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = __empty( 1, 2, 3 ); } $first = clock( 'empty function' ); $i = $iteration; while( --$i ) { $result = __small( 1, 2, 3 ); } $second = clock( 'small function' ); result( $first, $second, $iteration ); /** * is_null function vs. === null statement */ $iteration = 1000000; start( 'is_null', '=== null', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = is_null($i); } $first = clock( 'is_null' ); $i = $iteration; while( --$i ) { $result = $i===null; } $second = clock( '=== null' ); result( $first, $second, $iteration ); /** * split() function vs. explode() function */ $iteration = 1000000; start( 'split', 'explode', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = split( 'e', $shortString ); } $first = clock( 'split' ); $i = $iteration; while( --$i ) { $result = explode( 'e', $shortString ); } $second = clock( 'explode' ); result( $first, $second, $iteration ); /** * split() function vs. preg_split() function */ $iteration = 100000; start( 'split', 'explode', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = split( '[last]{2,}', $shortString ); } $first = clock( 'split' ); $i = $iteration; while( --$i ) { $result = preg_split( '/[last]{2,}/', $shortString ); } $second = clock( 'preg_split' ); result( $first, $second, $iteration ); /** * is_a() function vs. instanceof construct */ $iteration = 1000000; start( 'is_a', 'instanceof', null, $iteration ); $a = new stdClass(); $b = new TQObject(); clock( 'start', false, false ); $i = $iteration; while( --$i ) { if( is_a( $a, 'stdClass') ); } $first = clock( 'is_a' ); $i = $iteration; while( --$i ) { if( $a instanceof stdClass ); } $second = clock( 'instanceof' ); result( $first, $second, $iteration ); /** * ereg_replace() function vs. preg_replace() function */ $iteration = 100; start( 'ereg_replace', 'preg_replace', 'run on a string with 6818 chars', $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = ereg_replace( '[last]{2,}', '#', $longString ); } $first = clock( 'ereg_replace' ); $i = $iteration; while( --$i ) { $result = preg_replace( '/[last]{2,}/', '#', $longString ); } $second = clock( 'preg_replace' ); result( $first, $second, $iteration ); /** * ereg_replace() function vs. preg_replace() function */ $iteration = 1000; start( 'ereg_replace', 'preg_replace', 'run on a string with 681 chars', $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = ereg_replace( '[last]{2,}', '#', $mediumString ); } $first = clock( 'ereg_replace' ); $i = $iteration; while( --$i ) { $result = preg_replace( '/[last]{2,}/', '#', $mediumString ); } $second = clock( 'preg_replace' ); result( $first, $second, $iteration ); /** * preg_match() function vs. str_replace() function */ $iteration = 10000; start( 'ereg_replace', 'str_replace', 'run on a string with 681 chars', $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = ereg_replace( 'is', '#', $mediumString ); } $first = clock( 'ereg_replace' ); $i = $iteration; while( --$i ) { $result = str_replace( 'is', '#', $mediumString ); } $second = clock( 'str_replace' ); result( $first, $second, $iteration ); /** * date() function vs. DateTime object */ $iteration = 100000; start( 'date', 'DateTime', 'DateTime has been instanciated upfront loop!', $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = date( 'Y-m-d H:i:s' ); } $first = clock( 'date' ); $i = $iteration; while( --$i ) { $result = $dateTimeNow->format( 'Y-m-d H:i:s' ); } $second = clock( 'DateTime' ); result( $first, $second, $iteration ); /** * if statement vs. ternary */ $iteration = 1000000; start( 'if', 'ternary', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { if( $i > 0 ) { $result = 5; } else { $result = 4; } } $first = clock( 'if' ); $i = $iteration; while( --$i ) { $result = $i > 0 ? 5 : 4; } $second = clock( 'ternary' ); result( $first, $second, $iteration ); /** * if statement vs. ternary */ $iteration = 1000; start( 'serialize', 'json_encode', null, $iteration ); clock( 'start', false, false ); $i = $iteration; while( --$i ) { $result = serialize($largeArray); } $first = clock( 'serialize' ); $i = $iteration; while( --$i ) { $result = json_encode($largeArray); } $second = clock( 'json_encode' ); result( $first, $second, $iteration );
Output for 5.3.0 - 7.1.0
Warning: require(_functions.php): failed to open stream: No such file or directory in /in/tXl8Z on line 5 Fatal error: require(): Failed opening required '_functions.php' (include_path='.:') in /in/tXl8Z on line 5
Process exited with code 255.