Friday, October 08, 2004

more about optimization

I've just found Ilia Alshanetsky's blog and a very usefull article: PHP Optimization Tricks

Friday, September 10, 2004

Performance & tweaking : FOR vs. WHILE vs. DO-WHILE

just out of curiosity I wanted to see which one of these structures are faster. I believed that "for" will be the fastest but the tests proved I was wrong.
Anyway here is my code so you could try it yourselvs:


<?php
function getmicrotime()
{
   list(
$usec, $sec) = explode(" ", microtime());
   return ((float)
$usec + (float)$sec);
}

$count = 2000000;
$startFor = getmicrotime();
for (
$i=0; $i<$count; $i++)
{
    
//do nothing
}
$stopFor = getmicrotime();
echo
"\n<br> Time for for:",$stopFor-$startFor;

$startWhile = getmicrotime();
$i=0;
while (
$i<$count)
{
    
$i++;
}
$stopWhile = getmicrotime();
echo
"\n<br> Time for while:",$stopWhile-$startWhile;

$startDo = getmicrotime();
$i=0;
do
{
    
$i++;
}
while (
$i < $count);
$stopDo = getmicrotime();
echo
"\n<br> Time for do:",$stopDo-$startDo;
?>



And here are the times:
for: 2.078712940216064
while: 1.981828927993774
do: 1.936743974685669

As you can see for 2,000,000 iterations the do-while structure is almost 0.15 seconds faster than the for structure. This won't mean very much for small sites but in complex aplication where you'll have to handle many hits it will make a difference

Saturday, September 04, 2004

GtkCList with sort support

this is my first php-gtk related post on this blog.
bellow you ca find a class derived from GtkClist to which I've added sort cappabilities for columns.


<?php
class IList extends GtkCList
{
    var
$lastSortColumn; //last sorted column
    
var $lastSortType; //last sort type
    
var $labels; //column titles
    
    /**
    * @return IList
    * @param array $labels
    * @desc constructor
    */
    
function IList($labels)
    {
        
GtkCList::GtkCList(count($labels), $labels);
        
$this->labels = $labels;
        
GtkCList::connect("click_column", array(&$this, "_sortColumn"));
    }
    
    
/**
    * @return private
    * @param IList $list
    * @param int $column
    * @desc sorts the column
    */
    
function _sortColumn($list, $column)
    {
        if (
$column == $this->lastSortColumn)
        {
            
$this->lastSortType = ($this->lastSortType) ? false : true;
        }
        else
        {
            
$this->lastSortType = true;
        }
        
IList::_setSortColumn($column, $this->lastSortType);
        
        
GtkCList::set_sort_type($this->lastSortType ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING);
        
$GLOBALS["curentListSort"] = array($this->lastSortColumn, $this->lastSortType);
        
GtkCList::set_sort_column($this->lastSortColumn);
        
GtkCList::sort();
    }
    
    
/**
    * @return void
    * @param int $column
    * @param boolean $ascending
    * @desc
    */
    
function _setSortColumn($column, $ascending = true)
    {
        if (!
is_null($this->lastSortColumn))
        {
            
$widget = GtkCList::get_column_widget($this->lastSortColumn);
            
$widget->destroy();
            for (
$i=0; $i<count($this->labels); $i++)
            {
                
GtkCList::set_column_title($i, $this->labels[$i]);
            }
        }
        
$hbox = &new GtkHBox();
        
$label = &new GtkLabel($this->labels[$column]);
        if (
$ascending === true)
        {
            
$image = &new GtkArrow(GTK_ARROW_UP, GTK_SHADOW_ETCHED_IN);
        }
        else
        {
            
$image = &new GtkArrow(GTK_ARROW_DOWN, GTK_SHADOW_ETCHED_IN);
        }
        
        
$hbox->pack_start($label, false, false,0);
        
$hbox->pack_end($image, false, false,0);
        
$hbox->show_all();
        
GtkCList::set_button_actions(1, 1);
        
GtkCList::set_shadow_type(GTK_SHADOW_OUT);
        
GtkCList::set_column_widget($column, $hbox);
        
$this->lastSortColumn = $column;
    }
}
?>



... more to come. stay close

Friday, September 03, 2004

Disclaimer

English is not my native language, so if you find spelling errors or stuff like that ignore them.
All information presented in this site is for educational purposes only.
If you find this blog usefull recommend it to a friend or send a "thank you" email to the PHP-GTK dev team