Reply to Text substitution timing tests...

Your name:

Reply:


Posted by Curtis on 12/15/05 21:24

I'm using 5000 characters, with a few of each of the target
letters scattered throughout the text. PHP 4.2.3 on a WAMP
setup ~500MHz. The puzzling results for me are D and E,
where 1) dropping the presence test results for
known-present characters results in a slow-down, and 2) the
switch construct is about 800 times slower than serial IF
statements.

Fastest results listed first. Comments about the whys and
wherefors appreciated.

--

Curtis

Visit We the Thinking
www.wethethinking.com
An online magazine/forum
devoted to philosophical
thought.

############################################################
########
#
# A. 5.2 seconds for 20,000 iterations
#
# NONE of these characters used in the text.
#
############################################################
########

if (strstr($text, "z")) $text = str_replace("z", "<br>#",
$text);
if (strstr($text, "2")) $text = str_replace("2", "<br>#",
$text);
if (strstr($text, "x")) $text = str_replace("x", "<br>#",
$text);
if (strstr($text, "K")) $text = str_replace("K", "<br>#",
$text);
if (strstr($text, "Z")) $text = str_replace("Z", "<br>#",
$text);
if (strstr($text, "X")) $text = str_replace("X", "<br>#",
$text);
if (strstr($text, "Q")) $text = str_replace("Q", "<br>#",
$text);

############################################################
########
#
# B. 5.6 seconds for 20,000 iterations
#
# ALL of these characters used several times in the text.
#
############################################################
########

if (strstr($text, "P")) $text = str_replace("P", "<br>#",
$text);
if (strstr($text, "M")) $text = str_replace("M", "<br>#",
$text);
if (strstr($text, "L")) $text = str_replace("L", "<br>#",
$text);
if (strstr($text, "I")) $text = str_replace("I", "<br>#",
$text);
if (strstr($text, "N")) $text = str_replace("N", "<br>#",
$text);
if (strstr($text, "E")) $text = str_replace("E", "<br>#",
$text);
if (strstr($text, "A")) $text = str_replace("A", "<br>#",
$text);

COMMENTS: Using the test for the character before the
substitution
resulted in a modest time savings when characters stand a
chance of
being absent. Using the test ahead of a preg_replace can
result in
GREAT speed increase, depending upon content.

############################################################
########
#
# C. 13 seconds for 20,000 iterations
#
# ALL of these characters used several times in the text.
#
############################################################
########

Setup:

$letters = array("P", "M", "L", "I", "N", "E", "A");
$subst = "<br>#";

In the interative loop:

$text = str_replace($letters, $subst, $text);

COMMENTS: This is a bit slower than I'd expected.

############################################################
########
#
# D. 16 seconds for 20,000 iterations
#
# ALL of these characters used several times in the text.
#
############################################################
########

$text = str_replace("P", "<br>#", $text);
$text = str_replace("M", "<br>#", $text);
$text = str_replace("L", "<br>#", $text);
$text = str_replace("I", "<br>#", $text);
$text = str_replace("N", "<br>#", $text);
$text = str_replace("E", "<br>#", $text);
$text = str_replace("A", "<br>#", $text);

COMMENTS: I don't understand this. Can anyone explain
why skipping the tests for the presence of a character
would result in a slowdown for text where the character
is known to be present? (Compare D with B.)

############################################################
########
#
# E. 5.6 seconds for 25 iterations
#
# ALL of these characters used several times in the text.
#
############################################################
########

function subst($str)
{
$tmp = "";
for($i = 0; $i < strlen($str); $i++)
{
switch($str[$i])


case "P":
$tmp .= "<br>#"; break;
case "M":
$tmp .= "<br>#"; break;
case "L":
$tmp .= "<br>#"; break;
case "I":
$tmp .= "<br>#"; break;
case "N":
$tmp .= "<br>#"; break;
case "E":
$tmp .= "<br>#"; break;
case "A":
$tmp .= "<br>#"; break;
default:
$tmp .= (string)$str[$i]; break;
}
}
return($tmp);
}

COMMENTS: I can't imagine why the switch construct would be
slower
by a factor of 800!!

END

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация