notes.io background
what is notes.io
Fast | Easy | Short

Fast | Easy | Short

PHP TIPS

PHP SETTER or GETTER Speed Test


Avoid writing naive setters and getters

Setting and calling the name property directly can run up to 100% faster, as well as cutting down on development time.

codes.......




php -d implicit_flush=off -r 'class dog {public $name = "";public function setName($name) {$this->name = $name; }public function getName() {return $this->name; } }$rover = new dog();for ($x=0; $x<10; $x++) { $t = microtime(true);for ($i=0; $i<1000000; $i++) { $rover->setName("rover");$n = $rover->getName();}echo microtime(true) - $t;echo "n";}'
1.48462200165
1.49136686325
1.48365998268
1.47310495377
1.46616101265
1.44583415985
1.42663908005
1.43124985695
1.42830300331
1.42891597748


php -d implicit_flush=off -r 'class dog { public $name = "";} $rover = new dog(); for ($x=0; $x<10; $x++) { $t = microtime(true); for ($i=0; $i<1000000; $i++) { $rover->name = "rover"; $n = $rover->name;} echo microtime(true) - $t;echo "n";}'
0.700392007828
0.686674118042
0.687913894653
0.693347930908
0.697072982788
0.708423852921
0.709672927856
0.704964876175
0.704661130905
0.708118915558




When writing classes in PHP, you can save time and speed up your scripts by working with object properties directly, rather than writing naive setters and getters. In the following example, the dog class uses the setName() and getName() methods for accessing the name property.




twitter.com/ugurethemaydin
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others by providing the shorten url to a friend.


Fast: Notes.io is built for speed and performance. You can take notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share shorten link!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q

Contact: hello@notes.io

     
 

Shortened Note Link

 
 
Looding Image
 
     
 

Long File

 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.