Template Speed Test, F3 & Haanga

Developers
2011-04-28
2012-09-25
  • Stu Warneski

    Stu Warneski - 2011-04-28

    Simple Multi-Dimensional Array loop

    tested on my lame Pentium3, 450Mhz, 317M RAM, your results SHOULD be faster
    ::)
    I hope this displays correctly
    & I hope my method of testing is valid.

    F3
    elapsed time for template serving: 0.267seconds


    elapsed time for entire script: 0.354seconds

    memory used: 0.227MB

    Haanga
    elapsed time for template serving: 0.012seconds


    elapsed time for entire script: 0.024seconds

    memory used: 0.225MB

    F3
    GET /templatetest/50 for 50 rows, valid values from 10 to 1,000

    index.php

    <?php
    // F3 example of multi-dimensional template test
    
    $app=require(__DIR__.'/lib/base.php');
    
    $app->set('AUTOLOAD','inc/');
    $app->set('CACHE',TRUE);
    $app->set('DEBUG',1);
    $app->set('EXTEND',TRUE);
    $app->set('GUI','gui/');
    
    $app->route('GET /templatetest*', function() 
    {   // start timer of function
        $time_start = microtime(TRUE);
        // get memory used before function
        $mem_start = memory_get_usage();
    
        //Get any specified row count after the second slash, if nothing, set rows = 50
        $rows_a = explode('/',F3::get('PARAMS.0') ) ;
        count($rows_a) == 3 ? $rows = $rows_a[2] : $rows = 50;
        // make sure positive int between 10 and 1,000
        $rows = filter_var($rows, FILTER_VALIDATE_INT, array('options'=>array('default'=>50,'min_range'=>10,'max_range'=>1000) ) );
    
        $data = array();
        for($i = 0; $i < $rows; $i++) 
        {
            $data[] = array('id' => $i, 'name' => "name {$i}");
        }
    
        F3::set('number', $rows);
        F3::set('title', 'F3 Template Speed Test');
        F3::set('table', $data);
    
        // count elapsed time just for template rendering
        $ttime = microtime(TRUE);
        $rendered_output = Template::serve('f3template.html');
        echo "elapsed time for template serving: " . round(microtime(TRUE)-$ttime,3) . "seconds<hr>";
        echo "elapsed time for entire script: " . round(microtime(TRUE)-$time_start,3) . "seconds<hr>";
        echo "memory used: " . round( (memory_get_usage()-$mem_start) / (1024 * 1024), 3) . "MB<hr>";
        echo $rendered_output;
    }
    
    );
    
    $app->run();
    

    f3template.html

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <h1>{@title}</h1>
    
    <p>Table with {@number} rows.</p>
    
    <table border="1">
    <tr>
        <td><b>ID</b></td>
        <td><b>NAME</b></td>
    </tr>
    <F3:repeat group="{@table}" value="{@array_row}">
    <tr>
    <td>{@array_row.id}</td>
    <td>{@array_row.name}</td>
    </tr>
    <F3:repeat>
    </table>
    </body>
    </html>
    

    Haanga

    GET index_haanga.pgp?r=50 for 50 rows, valid values from 10 to 1,000
    index_haanga.php

    <?php
    // haanga test example
    
    // get time at start of script
    $time_start = microtime(TRUE);
    // get memory used before function
    $mem_start = memory_get_usage();
    
    require(__DIR__."/lib/Haanga.php");
    
    // set Haanga configuration values
    $config = array(
    'cache_dir' => 'hgtemp/',
    'template_dir' => 'hgtmpl/',
    );
    // set the config values into the class through a static method
    Haanga::Configure($config);
    
    // create the multi-dimensional array
    $rows = filter_var($_GET['r'], FILTER_VALIDATE_INT, array('options'=>array('default'=>50,'min_range'=>10,'max_range'=>1000) ) );
    
    $data = array();
    for($i = 0; $i < $rows; $i++) 
    {       $data[] = array('id' => $i, 'name' => "name {$i}");
    }
    
    //set the value of variables to be passed to the template. it is passed as an array, because it is more than 1 variable
    $vars = array
    (
    'number'=>$rows,   // a variable named 'number' with value is the php variable $rows, set above
    'title'=>'haanga', // a variable named 'title'  with value of 'haanga' 
    'table'=> $data,   // a variable named 'table'  with value of the array named 'data'
    );
    
    // start timer of template swap
    $ttime = microtime(TRUE);
    
    // load template and send variables to it, assign output to $out var
    $rendered_output = Haanga::Load('haanga_test.html', $vars, true );
    
    // get stats
    echo "elapsed time for template serving: " . round(microtime(TRUE)-$ttime,3) . "seconds<hr>";
    echo "elapsed time for entire script: " . round(microtime(TRUE)-$time_start,3) . "seconds<hr>";
    echo "memory used: " . round( (memory_get_usage()-$mem_start) / (1024 * 1024), 3) . "MB<hr>";
    $rendered_output;
    
    // display template
    echo $rendered_output;
    

    Haanga Template \hgtmpl\haanga_test.html

    <!--
    template file
    title is a variable
    number is a variable
    table is a variable which equals the multidimensional array $data
    
    for row in table,
    means it will loop thru the multi-dimensional array represented by the variable "table"
    this array is made of up key/values, 
    the key is just a number that represents the position of the element in the array (0,1,2, etc)
    the value is a nested array,
    the nested array has 2 elements, id and name
    
    so for each row in the array named table
    call the row, 
    and get the value of the key named 'id'
    and get the value of the key named 'name'
    
    -->
    
    <h1>{{title}}</h1>
    
    <p>Table with {{number}} rows.</p>
    
    <table border="1">
    <tr>
        <td><b>ID</b></td>
        <td><b>NAME</b></td>
    </tr>
    {% for row in table %}
    
    <tr>
        <td>{{row.id}}</td>
        <td>{{row.name}}</td>
    </tr>
    {% endfor %}
    </table>
    
     
  • Stu Warneski

    Stu Warneski - 2011-05-04

    Results are in for the new improved F3 template engine!

    same code as above, template changes to use {{@varname}} format

    Table with 50 rows:
    first time:
    template renders in 0.035681 seconds
    elapsed time for entire script: 0.115777 seconds
    memory used: 0.140804MB

    second time (uses cached php compiled scripts):
    template: 0.016011 secs
    entire script: 0.09609 secs
    mem: 0.140545mb

    old results for easy comparison:
    F3
    elapsed time for template serving: 0.267seconds
    elapsed time for entire script: 0.354seconds
    memory used: 0.227MB

    this is on a very old machine, it does not use the data:// protocol (i
    couldn't get the to work yet)
    and it does NOT use any apc caching.

    so WOW ! an awesome improvement for F3 v2 r131 !

    good job Bong.

     
  • Stu Warneski

    Stu Warneski - 2011-05-05

    template speed results with Rev 132:

    elapsed time for template serving: 0.014354seconds

    elapsed time for entire script: 0.093422seconds

    memory used: 0.136494MB

     
  • Bong Cosca

    Bong Cosca - 2011-05-05

    Given the new speed results, maybe a fair comparison of features offered by
    the engines would be something to look at.

     
  • Stu Warneski

    Stu Warneski - 2011-05-05

    yes, good idea, someday. i just want to make a web app with f3 first and get
    it done. maybe after that i can add more content to this