A thread for when you'ce got that nasty sluggish little bottleneck into an tarmac smouldering speed machine, or something similar.
As I'm sure you can guess, the reason I've started this thread is because I've done one such thing. It wasn't particularly slow to begin with but but now it's particularly not slow. It's a function to find what letters are in a string and count the occurence of each (I use this kind off thing for my fuzzy search stuff).
What I had originally looks a little sloppy but I couldn't think of any other way of doing it.
function getLetters($string)
{
$letters=array();
for($i=0;$i<strlen($string);$i++) {
if(array_key_exists($string{$i},$letters)) {
$letters[$string{$i}]++;
} else {
$letters[$string{$i}]=1;
}
}
return $letters;
}
But I managed to speed it up by an order of 10.24 (a mean taken over 100 iterations of a string 322000bytes long)
function getLetters($string)
{
while(strlen($string)>0) {
$letters[$word{0}]=count_chars($word,$word{0});
$word=str_replace($word{0},'',$word);
}
return $letters;
}
Any ideas optimize this further would be great.
Hope to see more Speedy Gonzalez(sp?) bits of code.
Bubble