php cli script with if-then's very slow

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've noticed that php cli scripts using for-loops with some if...then's are very slow using the php cli (command line interface). The following php script takes somewhat 100 seconds (php v5.0.3 on a redhat linux, 3 Ghz PC). The same program in c (see below) less than 1 second ... I know that php is a scripting language, but isn't the difference between de c compiled prog and de php script not very extreme ?

Frans

php script :

#!/usr/bin/php
<?php

$a=0;
$r=100;

for($p=1;$p<=3;$p++)
{
  for ($c=1; $c<=1230; $c++)
  {
    for ($j=0;$j<=23;$j++)
    {
     for ($z=0; $z<=$r; $z++)
     {
      $a++;
      if($p==1 && $c!=500 && $j==22)
      {
       $a--;
      }
      if($p==2 && $c!=600 && $j==21)
      {
       $a--;
      }
      if($p==3 && $c!=700 && $j==20)
      {
       $a--;
      }
      if($p==1 && $c!=800 && $j==19)
      {
       $a--;
      }
      if($p==2 && $c!=900 && $j==18)
      {
       $a--;
      }
     }
    }
   }
 }
  printf("a=%d\n",$a);

  exit(0);


?>


c-prog :

main()
{

int a,r,p,z,j,c;

a=0;
r=100;

for(p=1;p<=3;p++)
{
  for (c=1; c<=1230; c++)
  {
    for (j=0;j<=23;j++)
    {
     for (z=0; z<=r; z++)
     {
      a++;
      if(p==1 && c!=500 && j==22)
      {
       a--;
      }
      if(p==2 && c!=600 && j==21)
      {
       a--;
      }
      if(p==3 && c!=700 && j==20)
      {
       a--;
      }
      if(p==1 && c!=800 && j==19)
      {
       a--;
      }
      if(p==2 && c!=900 && j==18)
      {
       a--;
      }
     }
    }
   }
 }
  printf("a=%d\n",a);

  exit(0);

}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux