
- Image via Wikipedia
Neulich beim refaktorieren: Ich iteriere ein Array und bei einem bestimmten Index führe ich eine bestimmte Aktion aus. Wie führe ich die Aktion aus, als statische Methode, als Methode nach einer Objektinstanzierung oder als Funktion.Man weiss ja dass Funktionen schneller sind als statische Methoden und diese wiederum schneller als Methoden, doch wie schnell?
Folgendes Skript verdeutlicht die Performanceunterschiede:
class Test {
/**
* @return The result of 2 + 2 calculation
*/
function testMethod() {
return 2 + 2;
}
}
// A function that returns two plus two
function testFunction() {
return 2 + 2;
}
// this will hold the result of callng the method/function
$m = 0;
echo '
| Static method | Method with Object creation | Method | Function | Method vs static bench | Method with Object creation vs static bench | Function vs static bench |
|---|---|---|---|---|---|---|
| ' . $static_time . ' | '; // call the method "normally" $t = new Test ( ); // create the object outside the loop $start = microtime (); for($i = 0; $i < 10000; $i ++) { $m = $t->testMethod (); } $method_time = microtime () - $start; echo '' . $method_time . ' | '; // crete an object inside the loop and call the method $start = microtime (); for($i = 0; $i < 10000; $i ++) { $t2 = new Test ( ); $m = $t2->testMethod (); } $mo_time = microtime () - $start; echo '' . $mo_time . ' | '; // call the function that doeas the same $start = microtime (); for($i = 0; $i < 10000; $i ++) { $m = testFunction (); } $fn_time = microtime () - $start; echo '' . $fn_time . ' | '; // ratios echo '' . (($method_time / $static_time) * 100) . ' | '; echo '' . (($mo_time / $static_time) * 100) . ' | '; echo '' . (($fn_time / $static_time) * 100) . ' | '; echo '
Und das Resultat:
Static Method with Method Method vs Method with Function vs
method Object Function static Object creation static
creation bench vs static bench bench
------------------------------------------------------------------------------------------------
0.016017 0.016807 0.029788 0.008337 104.932259474 185.977399014 52.05094587
0.01513 0.016974 0.048973 0.014039 112.187706543 323.681427627 92.7891606081
0.025161 0.028501 0.049813 0.013888 113.274512142 197.97702794 55.196534319
0.025065 0.027126 0.050599 0.013764 108.222621185 201.871135049 54.9132256134
0.024057 0.028618 0.050516 -0.991544 118.959138712 209.984619861 -4121.64442782
0.015437 0.01702 0.039125 0.015113 110.254583144 253.449504437 97.9011465958
0.02635 0.032555 0.049082 0.013597 123.548387097 186.269449715 51.6015180266
0.024788 0.027642 0.049174 0.013925 111.51363563 198.378247539 56.1763756656
0.025098 0.027309 0.050644 0.013834 108.80946689 201.785002789 55.1199298749
0.02432 0.028128 0.049716 0.01384 115.657894737 204.424342105 56.9078947368
0.025347 0.028046 0.049223 0.013674 110.648202943 194.19655186 53.9472126879
0.024709 0.026775 0.051406 0.00861 108.361325833 208.045651382 34.8456028168
0.015097 0.017242 0.050361 0.015824 114.208120819 333.582831026 104.815526263
0.027345 0.029153 0.049606 -0.986188 106.611812031 181.407935637 -3606.465533
0.025246 0.027103 0.050693 0.013765 107.355620692 200.796165729 54.5234888695
0.024814 0.02865 0.049334 0.013214 115.459015072 198.815184976 53.2521963408
0.025116 0.028278 0.049423 0.013806 112.589584329 196.778945692 54.9689440994
0.024819 0.027729 0.049891 0.013886 111.72488819 201.019380313 55.949071276
0.025069 0.026437 0.041419 0.008819 105.456938849 165.21999282 35.1789062188
0.015648 0.024254 0.056992 0.014997 154.997443763 364.212678937 95.8397239264
Was lernen wir daraus:
- Eine statische Methode aufzurufen ist doppelt so schnell wie das Instanzieren eines Objektes und dann die Methode aufzurufen.
- Eine statische Methode ist doppelt so schnell als das Instanzieren eines Objektes und dann die Methode aufzurufen nachdme das Objekt bereits existiert.
- Funktionsaufrufe sind doppelt so schnell wie statische Methoden.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=e808faca-b426-436d-97e7-5ec846585200)