Last active
December 18, 2015 05:49
-
-
Save dantman/5735077 to your computer and use it in GitHub Desktop.
Benchmarking different types of html escapes. strtr vs various ways of doing str_replace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 52.938008308411µs. | |
| escape2: 11.997318267822µs. | |
| escape3: 11.176705360413µs. | |
| escape4: 11.816596984863µs. | |
| escape5: 10.962629318237µs. | |
| escape6: 12.953281402588µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 54.372382164001µs. | |
| escape2: 11.764526367188µs. | |
| escape3: 11.172556877136µs. | |
| escape4: 11.745500564575µs. | |
| escape5: 10.97309589386µs. | |
| escape6: 12.441611289978µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 52.889657020569µs. | |
| escape2: 12.288498878479µs. | |
| escape3: 11.119675636292µs. | |
| escape4: 11.369919776917µs. | |
| escape5: 11.594724655151µs. | |
| escape6: 12.19961643219µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 53.030753135681µs. | |
| escape2: 12.741279602051µs. | |
| escape3: 11.934113502502µs. | |
| escape4: 11.386919021606µs. | |
| escape5: 11.208868026733µs. | |
| escape6: 12.46395111084µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 52.644824981689µs. | |
| escape2: 13.211989402771µs. | |
| escape3: 11.271190643311µs. | |
| escape4: 11.585116386414µs. | |
| escape5: 10.786747932434µs. | |
| escape6: 11.802339553833µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 53.81031036377µs. | |
| escape2: 12.262892723083µs. | |
| escape3: 11.827492713928µs. | |
| escape4: 11.295986175537µs. | |
| escape5: 10.808277130127µs. | |
| escape6: 11.997056007385µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 52.564644813538µs. | |
| escape2: 12.166476249695µs. | |
| escape3: 11.496710777283µs. | |
| escape4: 11.321377754211µs. | |
| escape5: 11.368703842163µs. | |
| escape6: 11.893939971924µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 53.988575935364µs. | |
| escape2: 12.559056282043µs. | |
| escape3: 11.160278320312µs. | |
| escape4: 11.923813819885µs. | |
| escape5: 11.432695388794µs. | |
| escape6: 12.74847984314µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 56.198358535767µs. | |
| escape2: 12.325763702393µs. | |
| escape3: 11.355233192444µs. | |
| escape4: 11.871528625488µs. | |
| escape5: 11.633849143982µs. | |
| escape6: 13.079476356506µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 54.380536079407µs. | |
| escape2: 12.674951553345µs. | |
| escape3: 11.258745193481µs. | |
| escape4: 11.517882347107µs. | |
| escape5: 11.134505271912µs. | |
| escape6: 13.302636146545µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 54.559350013733µs. | |
| escape2: 12.682771682739µs. | |
| escape3: 12.019395828247µs. | |
| escape4: 11.4497423172µs. | |
| escape5: 11.379170417786µs. | |
| escape6: 12.645697593689µs. | |
| Daniels-MacBook-Air:5735077 daniel$ php test.php | |
| escape1: 54.48215007782µs. | |
| escape2: 12.672567367554µs. | |
| escape3: 11.710357666016µs. | |
| escape4: 11.998343467712µs. | |
| escape5: 11.019253730774µs. | |
| escape6: 12.479043006897µs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function escape1( $text ) { | |
| return strtr( $text, array( | |
| // There's no point in escaping quotes, >, etc. in the contents of | |
| // elements. | |
| '&' => '&', | |
| '<' => '<' | |
| ) ); | |
| } | |
| function escape2( $text ) { | |
| static $map = array( | |
| // There's no point in escaping quotes, >, etc. in the contents of | |
| // elements. | |
| '&' => '&', | |
| '<' => '<', | |
| ); | |
| static $keys, $values; | |
| if ( is_null( $keys ) ) { | |
| $keys = array_keys( $map ); | |
| $values = array_values( $map ); | |
| } | |
| return str_replace( $keys, $values, $text ); | |
| } | |
| function escape3( $text ) { | |
| // There's no point in escaping quotes, >, etc. in the contents of | |
| // elements. | |
| return str_replace( array( '&', '<' ), array( '&', '<' ), $text ); | |
| } | |
| function escape4( $text ) { | |
| static $keys = array( '&', '<' ); | |
| static $values = array( '&', '<' ); | |
| return str_replace( $keys, $values, $text ); | |
| } | |
| function escape5( $text ) { | |
| $text = str_replace( '&', '&', $text ); | |
| $text = str_replace( '<', '<', $text ); | |
| return $text; | |
| } | |
| function escape6( $text ) { | |
| $map = array( | |
| // There's no point in escaping quotes, >, etc. in the contents of | |
| // elements. | |
| '&' => '&', | |
| '<' => '<', | |
| ); | |
| return str_replace( array_keys( $map ), array_values( $map ), $text ); | |
| } | |
| $functions = explode( ' ', 'escape1 escape2 escape3 escape4 escape5 escape6' ); | |
| define( 'COUNT', 10000 ); | |
| foreach ( $functions as $func ) { | |
| $total = 0; | |
| for ( $i = COUNT; $i > 0; --$i ) { | |
| $start = microtime( true ); | |
| $func( '<p><b><a href="/wiki/Norman_Selfe" title="Norman Selfe">Norman Selfe</a></b> (1839–1911) was an Australian engineer, <a href="/wiki/Naval_architect" title="Naval architect" class="mw-redirect">naval architect</a>, inventor, <a href="/wiki/Urban_planning" title="Urban planning">urban planner</a> and outspoken advocate of technical education. After immigrating to <a href="/wiki/Sydney" title="Sydney">Sydney</a> with his family from England as a boy he became an apprentice engineer, following his father\'s trade. Selfe designed many bridges, docks, boats, and much precision machinery for the city. He also introduced new refrigeration, hydraulic, electrical and transport systems. For these achievements he received international acclaim during his lifetime. Decades before the <a href="/wiki/Sydney_Harbour_Bridge" title="Sydney Harbour Bridge">Sydney Harbour Bridge</a> was built, the city came close to building a Selfe-designed steel <a href="/wiki/Cantilever_bridge" title="Cantilever bridge">cantilever bridge</a> across the harbour after he won the second public competition for a bridge design. Selfe was commemorated in his lifetime by the name of the Sydney suburb of <a href="/wiki/Normanhurst,_New_South_Wales" title="Normanhurst, New South Wales">Normanhurst</a>. He was energetically involved in organisations such as the <a href="/wiki/Sydney_Mechanics%27_School_of_Arts" title="Sydney Mechanics\' School of Arts">Sydney Mechanics\' School of Arts</a> and the <a href="/wiki/Australian_Historical_Society" title="Australian Historical Society" class="mw-redirect">Australian Historical Society</a>, and fought consistently for the establishment of an independent system of technical education. He was acknowledged upon his death as one of the best-known people in, and greatest individual influences upon, the city of Sydney. (<a href="/wiki/Norman_Selfe" title="Norman Selfe"><b>Full article...</b></a>)</p>' ); | |
| $total += ( microtime( true ) - $start ); | |
| } | |
| echo "$func: ", ($total / COUNT) * 1000000, "µs.\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment