Page 1 of 1

Slow script

Posted: Mon May 27, 2002 4:57 am
by ILoveJackDaniels
Hiya, I've written this script to check the spelling of text sent from a form as $texttocheck. It runs really slowly, taking about a quarter of a second to check each word. Can anyone spot anything slowing this down badly, or suggest any ways I could change it to make it faster? Apologies in advance for the incompetent coding....

Code: Select all

while (preg_match("/  /",$texttocheck)) {
$texttocheck=str_replace("  "," ",$texttocheck);
}
while (preg_match("/\n/",$texttocheck)) {
$texttocheck=str_replace("\n"," <br> ",$texttocheck);
&#125;

$words_array=explode(" ",$texttocheck);
$wordnumber="0";
$link=@mysql_connect("localhost", "******", "*******");
@mysql_select_db("*******");
while ($words_array&#1111;$wordnumber]!="") &#123;
$word=$words_array&#1111;$wordnumber];
if ($word=="<br>") &#123; 
	echo " <br> ";
	$wordnumber++;
	continue;
&#125;
if (preg_match("/-/",$word)) &#123;
$words=explode("-",$word);
$number=0;
while ($words&#1111;$number]!="") &#123;
$wordtocheck=eregi_replace("&#1111;^a-z]","",$words&#1111;$number]);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number2=@mysql_num_rows($fromdb);
if ($number2=="0") &#123;
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$words&#1111;$number]" align="absmiddle">";
&#125; else &#123;
echo "$words&#1111;$number]";
&#125;
$number++;
if ($words&#1111;$number]!="") &#123; echo "-"; &#125;
&#125;
echo " ";
&#125; else &#123;
$wordtocheck=eregi_replace("&#1111;^a-z]","",$word);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number=@mysql_num_rows($fromdb);
if ($number=="0") &#123;
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$word" align="absmiddle"> ";
&#125; else &#123;
echo "$word ";
&#125;
&#125;
$wordnumber++;
&#125;
@mysql_close($link);

Posted: Mon May 27, 2002 6:23 am
by jason
Yup, pretty much, you are checking each word against a database, and using preg_* and ereg_* functions in loops. That will chew up a lot of time.

I would suggest one of several things.

First, stick to preg_* functions, they are faster.

Next, is their anyways you can cut down on the number of MySQL queries?

Have you considered optimizing your table? I don't know what the table structure looks like, what indexes you have, or what keys.

This might help.

Posted: Tue May 28, 2002 7:24 am
by ILoveJackDaniels
Thanks for the suggestions Jason. Unfortunately I have no idea how I would go about reducing the number of MySQL queries, other than listing common words (and, the, I, there, monkey etc) and having them skipped... Any ideas?

The preg functions seem to have sped it up a little too, and nothing can be changed really in the database, but I'm still playing around with things to change...

Posted: Tue May 28, 2002 11:08 am
by MattF
preg_replace accepts arrays of things to search for and replace, so why not try getting an array of all mis-spellings and their correct forms, eg

"functoins" => "functions" and run the whole text through that, it might take a while.....

Posted: Tue May 28, 2002 11:13 am
by jason
You might want to consider the aspell, or pspell extensions to PHP.