note 106796 added to function.preg-replace

From: Date: Wed, 07 Dec 2011 00:00:35 +0000
Subject: note 106796 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-184965@lists.php.net to get a copy of this message
I am building a little syntax highlighter for my personal use on a website and so was thinking of the best way to highlight words. On just the mysql related php words alone there are 48 different mysql commands (as of 06-Dec-2011). So it begs the question, 48 preg_replace's, 48 str_replaces or 1 massive preg_replace? In short, the one large preg_replace is faster, much faster. Here are the speed test results. Constant variables in test: <?php $text = "mysql_affected_rows mysql_client_encoding mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_escape_string mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_object mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_get_client_info mysql_get_host_info mysql_get_proto_info mysql_get_server_info mysql_info mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_processes mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_ping mysql_query mysql_real_escape_string mysql_result mysql_select_db mysql_set_charset mysql_stat mysql_tablename mysql_thread_id mysql_unbuffered_query"; $brightRedSpan = '<span style="color:#FF0000;">\\1</span>'; ?> 1 Large preg_replace: <?php $time1 = microtime(); /************************************************/ $reds = preg_replace( "/(mysql_ (affected_rows| client_encoding| close| connect| create_db| data_seek| db_(name|query)| drop_db| err(no|or)| escape_string| fetch_(array|assoc|field|lengths|object|row)| field_(flags|len|name|seek|table|type)| free_result| get_(client|host|proto|server)_info| info| insert_id| list_(dbs|fields|processes|tables)| num_(fields|rows)| p(connect|ing)| query| real_escape_string| result| select_db| set_charset| stat| tablename| thread_id| unbuffered_query))/sm", $brightRedSpan, $text); /************************************************/ $time2 = microtime(); $time_taken = $time2 - $time1; echo $time1.'<br/>'.$time2.'<br/>'; echo $time_taken; ?> The result was: 0.48501000 1323210912 0.48506700 1323210912 0.000057 48 single preg_replace's: <?php $time1 = microtime(); /************************************************/ $reds = preg_replace("/(mysql_affected_rows)/sm", $brightRedSpan, $text); $reds = preg_replace("/(mysql_client_encoding)/sm", $brightRedSpan, $reds); //and so forth to get them all (had to remove the rest to keep within allowed length of note) /************************************************/ $time2 = microtime(); $time_taken = $time2 - $time1; echo $time1.'<br/>'.$time2.'<br/>'; echo $time_taken; ?> The result was: 0.78061300 1323211559 0.78111600 1323211559 0.000503 48 single str_replace's: <?php $time1 = microtime(); /************************************************/ $reds = str_replace('mysql_affected_rows', '<span style="color:#FF0000;">mysql_affected_rows</span>', $text); $reds = str_replace('mysql_client_encoding', '<span style="color:#FF0000;">mysql_client_encoding</span>', $reds); //and so forth to get them all (had to remove the rest to keep within allowed length of note) /************************************************/ $time2 = microtime(); $time_taken = $time2 - $time1; echo $time1.'<br/>'.$time2.'<br/>'; echo $time_taken; ?> The result was: 0.05107100 1323213112 0.05146900 1323213112 0.000398 As you can see, the one large preg_replace is much faster than the 48 single str_replace's and much master still than the 48 single preg_replace's. The one large preg_replace is 6.98x faster than 48 single str_replace's. The one large preg_replace is 8.82x faster than 48 single preg_replace's. 48 single str_replace's are 1.26x faster than 48 single preg_replace's. Moral of the test. Use a single occurence of str_replace over a single occurence of preg_replace. But when you have to start issuing mulitple str_replace's in what could be done in one preg_replace, use the preg_replace. (I don't know at how many str_replace's it takes for the preg_replace to become quicker). ---- Server IP: 69.147.83.197 Probable Submitter: 2.100.28.101 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/106796 Del: integrated -- https://master.php.net/note/delete/106796/integrated Del: useless -- https://master.php.net/note/delete/106796/useless Del: bad code -- https://master.php.net/note/delete/106796/bad+code Del: spam -- https://master.php.net/note/delete/106796/spam Del: non-english -- https://master.php.net/note/delete/106796/non-english Del: in docs -- https://master.php.net/note/delete/106796/in+docs Del: other reasons-- https://master.php.net/note/delete/106796 Reject -- https://master.php.net/note/reject/106796 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184965) next »