Yii Framework Forum: Class-based vs static methods for HTML tag builders - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Class-based vs static methods for HTML tag builders A quick benchmark / comparison Rate Topic: -----

#1 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 03-September 09
  • Location:New York

Posted 11 January 2011 - 12:13 PM

I wanted to know approximately how much overhead would result from building HTML tags using a tag class, vs Yii's approach, where every tag builder is a static method.

Here's the benchmark I wrote:

<?php

/*

Tag class vs static tag builder benchmark.

Run using apache bench from the command-line:

  ab -n 1000 -c 10 http://test/sandbox/tagbench.php

*/

class Tag
{
  private $_name;
  private $_attr;
  
  public function __construct($name, $attr=array())
  {
    $this->_name = $name;
    $this->_attr = $attr;
  }
  
  public function __get($name)
  {
    return $this->_attr[$name];
  }
  
  public function __set($name, $value)
  {
    $this->_attr[$name] = $value;
  }
  
  public function __toString()
  {
    $attr = array();
    
    foreach ($this->_attr as $name=>$value)
      $attr[] = $name.'="'.$value.'"';
    
    return "<{$this->_name} ".implode(' ',$attr)."/>";
  }
}

class TagBuilder
{
  public static function build($_name, $_attr=array())
  {
    $attr = array();
    
    foreach ($_attr as $name=>$value)
      $attr[] = $name.'="'.$value.'"';
    
    return "<{$_name} ".implode(' ',$attr)."/>";
  }
}

header('Content-type: text/plain');

for ($i=0; $i<1000; $i++)
{
  // Comment out one version or the other before benchmarking.
  echo new Tag('img', array('src'=>'http://test/image.png', 'width'=>123, 'height'=>456));
  //echo TagBuilder::build('img', array('src'=>'http://test/image.png', 'width'=>123, 'height'=>456));
}


Measured result: the net overhead for the class-based version is 12%.

The advantages of using classes rather than static methods should be self-explanatory.

Do with this information what you will, I'm just putting that out there :-)
-3

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,433
  • Joined: 04-October 10

Posted 11 January 2011 - 12:19 PM

http://moisadoru.wor...on-call-in-php/
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com


Posted Image
0

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,433
  • Joined: 04-October 10

Posted 11 January 2011 - 12:28 PM

View PostAntonio Ramirez, on 11 January 2011 - 12:19 PM, said:



edit:

I have run your test with a 5000 loop and
* through static: 0.27240800857544
* through class based method: 0.36429977416992
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com


Posted Image
0

#4 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 03-September 09
  • Location:New York

Posted 17 January 2011 - 11:43 AM

View PostAntonio Ramirez, on 11 January 2011 - 12:28 PM, said:

edit:

I have run your test with a 5000 loop and
* through static: 0.27240800857544
* through class based method: 0.36429977416992


So by your measurement, a 33% overhead - I wonder why the overhead on your system is so much higher than on mine?

Did you change anything besides the number of loops before running your test?
0

#5 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,433
  • Joined: 04-October 10

Posted 18 January 2011 - 04:37 AM

No mindplay,

I was very curious when you post this info as I am building extensions to display adaptive Tags to my CMS style and you made me wonder. I used a bigger loop because in some cases the efficiency tend to decrease on the long run.

I am on a Macosx Snow Leopard 10.6.1, running Apache, Webgrind, and PHP 5.3.1

Could that affect?
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com


Posted Image
0

#6 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 03-September 09
  • Location:New York

Posted 18 January 2011 - 04:16 PM

View PostAntonio Ramirez, on 18 January 2011 - 04:37 AM, said:

No mindplay,

I was very curious when you post this info as I am building extensions to display adaptive Tags to my CMS style and you made me wonder. I used a bigger loop because in some cases the efficiency tend to decrease on the long run.

I am on a Macosx Snow Leopard 10.6.1, running Apache, Webgrind, and PHP 5.3.1

Could that affect?


Memory allocation/deallocation might be slower on OSX, that would slow down the object creation/destruction... just a guess, I really have no idea.

Are you using a bytecode cache? ... I don't see how that would really affect the result either, since that only affects parsing of the script, as far as I know.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users