FAQ |
Kalender |
![]() |
#1 | ||
|
|||
Nykomling
|
Vi säger att vi har denna classen :
Kod:
class DB { var $host; // The hostname var $user; // The username var $password; // The password var $database; // The database function DB($host,$user,$password,$database) // PHP4 class constructor { // populate the properties thats needed for connection $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } // lite andra metoder } då kan man ju göra såhär Kod:
class DB { var $host; // The hostname var $user; // The username var $password; // The password var $database; // The database function DB($host,$user,$password,$database) // PHP4 class constructor { // populate the properties thats needed for connection $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } function __construct($host,$user,$password,$database) // PHP5 class constructor { { $this->DB($host,$user,$password,$database); } // lite andra metoder } Kod:
class DB { var $host; // The hostname var $user; // The username var $password; // The password var $database; // The database function DB($host,$user,$password,$database) // PHP4 class constructor { // populate the properties thats needed for connection $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } function __construct($host,$user,$password,$database) // PHP5 class constructor { // populate the properties thats needed for connection $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } // lite andra metoder } Och ska man ta med PHP 5s construct och göra så att prestandan sjunker för PHP 4 användarna eller ska PHP 5 användarna få sämre prestanda ( då kör man utan PHP 5s construct |
||
![]() |
![]() |
![]() |
#2 | ||
|
|||
Mycket flitig postare
|
Självklart medför ett extra funktionsanrop att (när php5 konstruktorn anropar php4 konstruktorn) att det tar längre tid, men i praktiken så är nog deta en fis i havet jämfört med vad resten av sidan tar att parsa.
Skriv ett testscript som instansierar klassen flera tusen gånger och mät tiden så har du svaret. |
||
![]() |
![]() |
![]() |
#3 | ||
|
|||
Nykomling
|
hur var det nu man fick reda på execution time?
|
||
![]() |
![]() |
![]() |
#4 | |||
|
||||
Bara ett inlägg till!
|
Citat:
|
|||
![]() |
![]() |
![]() |
#5 | ||
|
|||
Mycket flitig postare
|
eller varför inte microtime...
microtime hjälpen på php.net har ett bra exempel. |
||
![]() |
![]() |
![]() |
#6 | |||
|
||||
Bara ett inlägg till!
|
Citat:
|
|||
![]() |
![]() |
![]() |
#7 | ||
|
|||
Nykomling
|
Gjorde lite benchmarks med dessa script
1. Kod:
<?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; class DB { var $host; var $user; var $password; var $database; function DB($host,$user,$password,$database) // PHP4 class constructor { $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } function __construct($host,$user,$password,$database) // PHP5 class constructor { $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } } for($i = 0; $i < 100000; $i++) { $instance = new DB('host','user','password','database'); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "execution time = $totaltime seconds<br/>instances maid = $i<br/>average execution time/instance = ".$totaltime / $i; ?> Kod:
<?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; class DB { var $host; var $user; var $password; var $database; function DB($host,$user,$password,$database) // PHP4 class constructor { $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } function __construct($host,$user,$password,$database) // PHP5 class constructor { $this->DB($host,$user,$password,$database); } } for($i = 0; $i < 100000; $i++) { $instance = new DB('host','user','password','database'); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "execution time = $totaltime seconds<br/>instances maid = $i<br/>average execution time/instance = ".$totaltime / $i; ?> Kod:
execution time = 2,03122901917 seconds instances maid = 100000 average execution time/instance = 0,0000203122901917 execution time = 2,01193809509 seconds instances maid = 100000 average execution time/instance = 0,0000201193809509 execution time = 1,98479390144 seconds instances maid = 100000 average execution time/instance = 0,0000198479390144 execution time = 1,99218416214 seconds instances maid = 100000 average execution time/instance = 0,0000199218416214 execution time = 2,01383900642 seconds instances maid = 100000 average execution time/instance = 0,0000201383900642 execution time = 2,13474011421 seconds instances maid = 100000 average execution time/instance = 0,0000213474011421 execution time = 1,98517394066 seconds instances maid = 100000 average execution time/instance = 0,0000198517394066 execution time = 2,01875305176 seconds instances maid = 100000 average execution time/instance = 0,0000201875305176 execution time = 2,08163309097 seconds instances maid = 100000 average execution time/instance = 0,0000208163309097 execution time = 2,01121997833 seconds instances maid = 100000 average execution time/instance = 0,0000201121997833 Total (10 executions): execution time = 20,26550436019 instances maid = 1000000 average execution time/instance = 0,00002026550436019 Kod:
execution time = 2,88538885117 seconds instances maid = 100000 average execution time/instance = 0,0000288538885117 execution time = 2,87395501137 seconds instances maid = 100000 average execution time/instance = 0,0000287395501137 execution time = 3,01868104935 seconds instances maid = 100000 average execution time/instance = 0,0000301868104935 execution time = 3,03595113754 seconds instances maid = 100000 average execution time/instance = 0,0000303595113754 execution time = 3,09631586075 seconds instances maid = 100000 average execution time/instance = 0,0000309631586075 execution time = 3,01692414284 seconds instances maid = 100000 average execution time/instance = 0,0000301692414284 execution time = 2,92553091049 seconds instances maid = 100000 average execution time/instance = 0,0000292553091049 execution time = 3,00412797928 seconds instances maid = 100000 average execution time/instance = 0,0000300412797928 execution time = 3,09196591377 seconds instances maid = 100000 average execution time/instance = 0,0000309196591377 execution time = 3,02725505829 seconds instances maid = 100000 average execution time/instance = 0,0000302725505829 Total (10 executions): execution time = 29,97609591485 instances maid = 1000000 average execution time/instance = 0,00002997609591485 Det går snabbare att ge egenskaperna ett värde igen än att kalla på PHP 4 konstruktorn |
||
![]() |
![]() |
![]() |
#8 | ||
|
|||
Mycket flitig postare
|
Citat:
Det du kallar slutsats är egentligen bara ett konstaterande/bevis för att något vi redan visste är sant. Diskussion: Jag valde snabbaste tiden för den snabba lösningen och den långsamaste för den långsamma. Detta ger en skillnad på 1,111 sekunder lite drygt. Detta då 100K instanser skapas. Detta ger en overhead på drygt 11 ms per instansiering. Samtidigt är ju lösningen där php5 konstruktorn anropar php4 konstruktorn lättare att underhålla eftersom förändringar bara behöver göras på ett ställe istf två. Slutsats: Man måste titta på hur mycket man tjänar i exekveringstid kontra eventuella problem med att underhålla två identiska funktioner. Personlig kommentar: Jag tvivlar på att du skapar 100K instanser per sida... och även om du gjorde det så hur stor del av hela sidans exekveringstid är då 1s? Du skall inte optimera saker som inte behöver optimeras. 11ms/anrop känns inte värt besväret med tanke på det du förlorar i underhållbarhet av koden. Möjligen är det värt det om du kansk spara flera sekunder per sida, men allvarligt talat - om du skapar ett par, tre hundra objekt per sida så känns det som något annat är fel i din design... |
||
![]() |
![]() |
![]() |
#9 | |||
|
||||
Bara ett inlägg till!
|
Citat:
|
|||
![]() |
![]() |
![]() |
#10 | ||
|
|||
Mycket flitig postare
|
Citat:
Och 11ms kan du inte säga är mycket utan att säga vad det jämförs med. 11ms av något som tar 3s är lite. 11ms av något som tar 12ms är mycket. |
||
![]() |
![]() |
Svara |
|
|