Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

php 5.3 - PHP Magic faster than simply setting the class attribute? -


well, not that, here example. can explain difference between b , c? how can faster use magic function dynamically set value instead of setting value in attribute definition?

here code:

[root@vm-202-167-238-17 ~]# cat test.php; d in b c; echo "------"; ./test.php $d; done; #!/usr/bin/php <?php  $classname = $argv[1];  class {     public function __get($a)     {         return 5;     } }  class b {     public $a = 5; }  class c {     public function __get($a)     {         $this->a = 5;          return 5;     } }  $a = new $classname;  $start = microtime(true);  ($i=0; $i < 1000000; $i++)     $b = $a->a;  $end = microtime(true);  echo (($end - $start) * 1000) ." msec\n";  ------ 598.90794754028 msec ------ 205.48391342163 msec ------ 189.7759437561 msec 

magic functions slower else in php, , should used carefully. blog subject (auto-creating attributes magic functions speed things up... anyway). el yobo stated, modified php script tests more accurate :

<?php  class {     public function __get($a) {         return 5;     } }  class b {     public $a = 5; }  class c {     private $a = 5;     public function __get($a) {         return $this->a;     } }  $classes = array('a','b','c');  header('content-type: text/plain; charset=utf-8'); foreach ($classes $classname) {    $a = new $classname;     $start = microtime(true);     ($i=0; $i < 1000000; $i++) {       $b = $a->a;    }     $end = microtime(true);     echo 'class ' . get_class($a) . ' = ' . (($end - $start) * 1000) ." msec\n"; } 

resulting in

class = 378.85212898254 msec class b = 109.26413536072 msec class c = 423.51794242859 msec 

so, there have it. can see magic functions, when used, take 4 times more execute public methods.

** edit **

now, if dynamically create new class attribute, magic method called first time, subsequent call access dynamically created public attribute (public backward compatibility). change class c :

class c {     public function __get($a) {         $this->a = 5;         return 5;     } } 

will output

class = 392.09413528442 msec class b = 110.16988754272 msec class c = 96.771955490112 msec 

so why : "hey! it's faster!" however, if reduce iterations 1000000 10 (for example):

class = 0.033140182495117 msec class b = 0.0078678131103516 msec class c = 0.01215934753418 msec 

class c slower b because it's initial call magic method. best guess php handles dynamically created attributes different declared ones. after further research, these results may vary depending on os, cpu arch, memory, php version, etc. therefore these results cannot taken granted, and, speaking, magic methods always take longer execute using declared public attributes or calling declared public methods.

** edit 2 **

here's class d test, skipping magic method whatsoever dynamic attribute creation :

class d {    public function __construct() {       $this->a = 5;    } } 

yields these results 1000 iterations :

class = 1.3999938964844 msec class b = 0.42200088500977 msec class c = 0.3960132598877 msec class d = 0.37002563476562 msec       <-- faster 

let's increase our iterations 1'000'000 :

class = 380.80310821533 msec class b = 109.7559928894 msec class c = 91.224908828735 msec        <-- faster ??? class d = 96.340894699097 msec 

if magic methods have great overhead cost, real question : why, when accessing same attribute repeatedly many times, is

public function __get($a) {    $this->a = 5;    return 5; } 

faster

public function __construct() {    $this->a = 5; } 

when creating , accessing dynamic attributes?


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -