Jump to content

reduce execution time in my script php


prince198

Recommended Posts

hi all

after hard battle with days in php and thank for all that they help me here . i finelly create functions with my sql that let me count number days between 2 days but in week and of course i can jump on years without bugs :D  but now i find that is take 10 secondes of time excecution in php. i tried smarty for cache systeme but no result

my function is :

 

<?php
function count_date($SAR)
{

$PARAM_hote='localhost'; // le chemin vers le serveur
$PARAM_name_bd='database'; // le nom de votre base de données
$PARAM_user='root'; // nom d'utilisateur pour se connecter
$PARAM_pass=''; // mot de passe de l'utilisateur pour se connecter
try
{
$connexion = new PDO('mysql:host='.$PARAM_hote.';dbname='.$PARAM_name_bd, $PARAM_user, $PARAM_mot_passe);
}

catch(Exception $e)
{
        echo 'Erreur : '.$e->getMessage().'<br />';
        echo 'N° : '.$e->getCode();
}
$sql=" select SAR, start_day, end_day from table"; 
$requete=$connexion-> query($sql);
$result = $requete -> fetchALL();
$To_days_start_final=0;
$first_week   = dayOfTheWeek(01, 2008, 0, "U"); 
$last_week   = dayOfTheWeek(52, 2008, "+5", "U"); 
$first_day__week=date("Y-m-d", $first_week );
$last_day__week=date("Y-m-d",$last_week);
$To_days_first_day__week=to_days($first_day__week);
$To_days_last_day__week=to_days(last_day__week);
$nbrdays1 []= 0;

foreach ($result as $row)
                   {
			   
			    
       $Brigade= $row['SAR']; 
                    $start_day = $row[start_day]; 
       $end_day = $row[end_day]; 
				/**********************************************************************************/
				if ($start_day < $first_day__week )
				{
				$start_day_final=$first_day__week ;
				}
				else
				{
				$start_day_final=$start_day_final;
				}
				/**********************************************************************************/
                  
                $To_days_start=to_days($start_day_final);
   $To_days_end=to_days($end_day);
   $Get_week  =date('W',strtotime("$start_day_final"));
   $week= trim($Get_week , 0);
   $day_week=date('N',strtotime("$start_day_final"));
				/***********************************************************************************/

   if( $To_days_end > $To_days_last_day__week)
         {
   $To_days_end_final = $To_days_last_day__week;
         }
         else
         {
         $To_days_end_final = $To_days_end;
         }
         if (ereg($SAR,$Brigade)) 

            for( $day=  $To_days_start; $jour <=$To_days_end_final; $day++ ) 
                           { 
					   
					   
                           if( $day_week >= 1 AND $day_week < 7 ) 
                           $nbrdays1[$week]++; 
		               $day_week++; 
                           if( $day_week > 7 ) 

			           {

			           $week++; $day_week = 1;
			     
				       }
				       }
                    }
				ksort($nbrdays1);
				print_r($nbrdays1);
				} 

 

 

for functions that u find here like to_days and first week is after helping of some friend here

there is adresse of topic

http://www.phpfreaks.com/forums/index.php/topic,205871.msg933951.html#msg933951

http://www.phpfreaks.com/forums/index.php/topic,206028.msg934850.html#msg934850

my script work now and i have result

for exemple if i have in my table

start_day: 2008-01-01

end day ;2008-01-09

will display

week 1: 5 days( because week 1 strat in 31 december 2007)

week 2: 3 days

but now in my tbale i have 2000 lines ans my script need 10 secondes to execute . so imagine if i have 10000 lines :-\

u know how i can reduce the time of excution and optimize it

thanks for help

Link to comment
Share on other sites

i think both

because as you see i save result in array

in my sql i take only start_day and end day

and in php i transform in function like to_days in mysql and i give all conditions to count niumber day by week in year

so i don't have really long time in mysql but because i have all this conditions in php take this 10 second and i'm afraid when i have 2000000 rows so i have to wait 2 min may be more

 

Link to comment
Share on other sites

Guest Xanza

Nothing you can really do to decrease the time of the mysql execution... But if you would like to decrease your overall page load time, look into gzipping your scripts. Keep in mind, your server has to support, and have it enabled.

 

If you want to look into it more, make sure your web host supports it, and enable it this way: http://elliottback.com/wp/archives/2006/01/12/http-gzip-compression-in-php/

 

Hope this helped.

Link to comment
Share on other sites

Guest Xanza

First make sure your host supports gzip compression, secondly:

 

In PHP, there are two easy ways to do this. The first is to add the following to your .htaccess file and let Apache do the rest:

 

    php_flag zlib.output_compression on

    php_value zlib.output_compression_level 2

 

The second is to add the following to the top of your php script file:

 

    ob_start(”ob_gzhandler”);

Link to comment
Share on other sites

Guest Xanza

....dude, this is the solution for something to be changed for your script to be optimized... But like I said, for the sql tables, their probably isn't anything you can do... Except some house-cleaning.

Link to comment
Share on other sites

The point of a database is to retrieve just the data you are interested in. You don't query for all the data and then use some slow parsed/tokenized/interpreted php code to scan through it to find or calculate the information you want.

 

You need to form your sql query so that it contains sql functions that directly select or calculate what you want and a WHERE clause that only selects the data you want. If you don't already know how to do this, you will need to get a basic mysql book to read or find and study some online tutorials.

 

If you describe specifically what your code is supposed to be doing (what the starting data looks like and what the results should be) and what your table definition is (it is possible that your table design is making the problem harder than necessary), someone can provide assistance.

Link to comment
Share on other sites

Hi,

 

PFMaBiSmAd is right. Gziping your script / output is basically pissing in the wind here and won't help you. Your program as is will time out with 100,000 rows in the database. I think you need to provide a detailed account of what you want your script to achieve, as your whole approach seems wrong.

 

Robin

Link to comment
Share on other sites

Guest Xanza
PFMaBiSmAd is right. Gziping your script / output is basically pissing in the wind here and won't help you.

 

Read my posts, I told him that it would not help his SQL... Only basic website load times... damn.

Link to comment
Share on other sites

Read my posts, I told him that it would not help his SQL... Only basic website load times... damn.

 

Wasn't meaning to attack your post, its just I really wanted to get across to OP that what he's doing is wrong on a fundamental level, so he needs to completely rethink his whole approach, not look for optimizations to what he currently has.

Link to comment
Share on other sites

hi evrybody and thank you for all response

i will explain my script:

we start from database like this

 

start_day   |     end_day |   work

2008-01-01|  2008-02-02|  morning

2007-12-25 | 2008-01-15 | evening

 

and now i have my script must to have result for example in year 2008 for person who work in evenig how many days he work but in week

so year 2008 start in mondy 31 december  if we want respect ISO...

so this person work in week 1(from 31december 2007 to 52008-01-05) 6 days

week 2 :6 days

week 3 : 2 days

of course i don't calculate sunday and week start in monday

 

so i hope now u unterstand workinf pof script and my database

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.