Welcome Guest, Not a member yet? Register   Sign In
Things that affect our applications' performance
#1

[eluser]KeyStroke[/eluser]
Hello,

What are the things, that we should avoid in our code, that usually affect our applications' performace negatively?

I thought I'd ask this question here since there are so many experienced prfoessional programmers. I want to make high-performing applications, but have no idea what could hurt their performance (i.e: slow them down).

It would be very helpful if you could share with us some tips based on your experience.
#2

[eluser]Popcorn[/eluser]
http://www.phpbench.com/

Smile
#3

[eluser]Derek Allard[/eluser]
Also, take a look at http://expressionengine.com/docs/development/index.html, and particularly the performance pages.
#4

[eluser]Seppo[/eluser]
Just a quick notice about that page (I have never seen it before)

Code:
foreach ($items as $name => $value)
{
    if (strpos($str, $name) !== FALSE)
    {
        str_replace($name, $value, $str);
    }
}
does not look optimal, as the guidelines say. If the string is not found, I'm still getting worst results...

And also,
Code:
$str = str_replace(array_keys($items), array_values($items), $str);
is at least two times faster...
#5

[eluser]Derek Jones[/eluser]
Seppo, can you share your code, environment details, and results? I don't see anything of the sort. strpos() if there are more matches than not is a tiny bit slower, but affords much greater speed when the replace item might not exist. And an array replacement with calls to fetch keys and values is always much much slower on every environment I have used, whether the replace strings exist or not, by orders of magnitude.
#6

[eluser]Aea[/eluser]
Ultimately the syntax differences will amount only to small differences in performance, most comes down to making sure your logic is as efficient as possible.
#7

[eluser]charlie spider[/eluser]
i remember reading a php article once about the speed of php versus databases where they benchmarked the differences between pulling a bunch of data from a db, storing it into an array ( or arrays ) and referencing the arrays whenever you need the data, versus making multiple and repeated calls to the database.

As i think anybody would expect, the benchmark testing proved that the php arrays performed much much faster than repeatedly accessing the database. We're talking two to three times faster, especially when you got into combining looping with databse calls, the arrays completely outperformed the database.

not really specific to CI but that may help change the way you approach dealing with your data.
#8

[eluser]Seppo[/eluser]
[quote author="Derek Jones" date="1214772129"]Seppo, can you share your code, environment details, and results? I don't see anything of the sort. strpos() if there are more matches than not is a tiny bit slower, but affords much greater speed when the replace item might not exist. And an array replacement with calls to fetch keys and values is always much much slower on every environment I have used, whether the replace strings exist or not, by orders of magnitude.[/quote]

Derek,
I've retested with a longer string (700 KB file) and I was thinking how to apologize Tongue. After that, I tested with a smaller string (186 KB) and I got a solution more like I expected, and with a more usual PHP string (like 100 chars or less) the results were exactly as I expected.
I'm running PHP 5.1.1 under Windows XP.

The code:
Code:
<?php
//$str = file_get_contents('/path/to/a/long/file');
$str = 'Hello! I am a small text. Hello! I am a small text. Hello! I am a small text.';
$items = array(
    'a' => 'b', // Many, many times
    'am' => 'd', // A few times
    'qweasdzxchiasfoihfiohioh' => '28946918yhsdouiahydoi' // Unlikely
);
foreach ($items as $key => $value)
{
    echo $key , ': ' , substr_count($str, $key) , "\n";
}
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    foreach ($items as $name => $value)
    {
        if (strpos($str, $name) !== FALSE)
        {
            str_replace($name, $value, $str);
        }
    }
}

echo 'Using strpos: ' , microtime(true) - $start;
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    foreach ($items as $name => $value)
    {
        str_replace($name, $value, $str);
    }
}
echo 'Not using strpos: ', microtime(true) - $start;
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    str_replace(array_keys($items), array_values($items), $str);
}
echo 'Using array: ', microtime(true) - $start;

With the 700kb file I'm getting:
Quote:a: 1190
am: 23
qweasdzxchiasfoihfiohioh: 0

Using strpos: 1.1060960292816
Not using strpos: 1.377170085907
Using array: 1.2317478656769

186KB
Quote:a: 674
am: 2
qweasdzxchiasfoihfiohioh: 0

Using strpos: 0.25286197662354
Not using strpos: 0.21404910087585
Using array: 0.22534990310669

'Hello! I am a small text. Hello! I am a small text. Hello! I am a small text.'
Quote:a: 9
am: 3
qweasdzxchiasfoihfiohioh: 0

Using strpos: 0.0062811374664307
Not using strpos: 0.0050199031829834
Using array: 0.0021741390228271
#9

[eluser]xwero[/eluser]
I've seen that behavior before. Benchmarking little strings is faster with functions than with control structures. But i wonder if the performance hit is noticeable for users?
#10

[eluser]Derek Jones[/eluser]
Quote:After that, I tested with a smaller string (186 KB) and I got a solution more like I expected, and with a more usual PHP string (like 100 chars or less) the results were exactly as I expected.

This exemplifies why some of these benchmarkings have to be taken with a rather large grain of salt, and that recommendations for coding practices (whether written by us for our developers or for general consumption) the context is just as important as the code used.

You're using the phrase "a more usual PHP string (like 100 chars or less) as if that's indicative of what all code will be operated on, and the results of those tests cannot be extrapolated linearly across all sizes of data (both string length and size of the $items array). Especially with the context of the EE 3rd party dev guidelines, the strings in question are template tagdata - for the CI analogy, think view files. Also, the find/replace items in these situations are more than replacing 'a' with 'b', but with replacing {some_variable} with who knows how much data.

In these situations, here is an example of results of a similar testing using data that EE developers commonly encounter:

Code:
$items['foo'] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$items['bar'] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$items['bat'] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
        
$haz = "Lorem ipsum {foo} dolor sit amet, consectetur {bar} adipisicing {bat} elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$doesnt_haz = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

Code:
Haz Strpos          0.2244
Haz Replace          0.1956
Haz Array Replace      0.7037

Doesnt Haz Strpos              0.0732
Doesnt Haz Replace          0.1379
Doesnt Haz Array Replace      0.3327

So I stand by our recommendations made in the EE dev guidelines.




Theme © iAndrew 2016 - Forum software by © MyBB