How to speed up the script?

There is a script
$questions_arr=json_decode(file_get_contents("base.json"),1);
array_change_key_case($questions_arr);
$input_text = $_GET['text'];
$input_text = mb_strtolower($input_text);


utf8_to_extended_ascii function($str, &$map)
{
 $matches = array();
 if (!preg_match_all('/[\xC0-\xF7][\x80-\xBF]+/', $str, $matches))
 return $str;
 foreach ($matches[0] as $mbc)
 if (!isset($map[$mbc]))
 $map[$mbc] = chr(128 + count($map));
 return strtr($str, $map);
}
levenshtein_utf8 function($s1, $s2)
{
 $charMap = array();
 $s1 = utf8_to_extended_ascii($s1, $charMap);
 $s2 = utf8_to_extended_ascii($s2, $charMap);
 return levenshtein($s1, $s2);
}

$shortest = -1;

foreach ($questions_arr as $q =>$reply) {
 $lev = levenshtein_utf8($input_text, $q);
 if ($lev == 0) {
 $closest = $q;
 $shortest = 0;
break;
}
 if ($lev <= $shortest || $shortest < 0) {
 $closest = $q;
 $shortest = $lev;
}
}
$answer = $questions_arr[$closest][rand(0, count($questions_arr[$input_text])-1)];
$sys = array(
 'Creator name' => '%creator_name%',
);
$answer = str_ireplace("%sys%", json_encode($sys), $answer);
if($answer == ""){
 $answer = "Shaw";
}
echo $answer;

Base 6 MB, everything works very long as speed up the work ?
June 14th 19 at 20:34
2 answers
June 14th 19 at 20:36
Lowenstein is one of the slowest algorithms. Try another one.
In General, look for bottlenecks. Remove timestamp on the different lines and see where and how much time script the most.
June 14th 19 at 20:38
1. Want to alphabetize the base itself.
2. To create an index file according to the type of Glossary of the book (ordered alphabetically and unique words from all text file or database) with reference to the desired row (s) in the database file.

Find more questions by tags PHP