How to reduce execution time in php

this is my script, it takes too long , how can I improve the execution time ?

SEE THE ALL OF THE COD: HERE !

$count_nr_attributes = count[$nume];
$csv[0] = $csv_array_attributes[0];
for [$i=0; $i < $csv_array_attributes; $i++] { 

        if [$i > 0]{
            $final = "";
            for [$j=1; $j < $count_nr_attributes; $j++] { 
                echo "select val_attr from attributes where id_produs = '$csv_array_attributes[$i]' and nume_attr = '$nume[$j]'";echo "
"; $select = mysql_query["select val_attr from attributes where id_produs = '$csv_array_attributes[$i]' and nume_attr = '$nume[$j]'"]; $count = mysql_num_rows[$select]; if [$count == 1]{ $row = mysql_fetch_array[$select]; $final .= $row['val_attr']."%%"; }else{ $final .= "no%%"; } } echo ""; } $csv[$i] = $csv_array_attributes[$i]."%%".$final; } //create CSV $file = fopen["attributes.csv","w+"]; foreach [$csv as $line]{ fputcsv[$file,explode['%%',$line],"^","`"]; }

The variable $count_nr_attributes contains more then 2500 values and also the $csv_array_attributes contains more then 2500 values. Actually i have two for loops and the execution time takes too long. How can i improve this ? thx

and the query result always return me one value;

asked Oct 15, 2014 at 7:46

6

Each time you're executing a SQL Query, no matter how many result it returns, it takes some time.

The best you can do to decrease the time your script takes, is to put the query outside the loops, fetch all the results at once, and process it afterward with your loop.

answered Oct 15, 2014 at 7:53

blue112blue112

48.6k3 gold badges45 silver badges54 bronze badges

2

First of all: don't use mysql_*. Anyway try to redo this with as few queries as possible. I don't have time to analyze all the intentions right now but try something like that:

$count_nr_attributes = count[$nume];
$csv[0] = $csv_array_attributes[0];

$query = "
SELECT id_produs, nume_attr, val_attr FROM attributes WHERE 
id_produs IN [".implode[",", $csv_array_attributes]."] AND nume_attr BETWEEN 0 AND {$count_nr_attributes} 
ORDER BY id_produs";

$res = mysql_query[$query];
$lastProduct = -1;
while[$row = mysql_fetch_array[$res]] {
  //do whatever you want
  //just check if id_produs is different than the previous one to emulate
  //$csv[$i] = $csv_array_attributes[$i]."%%".$final;
}

answered Oct 15, 2014 at 8:08

Tomasz KapłońskiTomasz Kapłoński

1,2502 gold badges20 silver badges45 bronze badges

3

use associative array with key as the id_produs and its value after that inside loop use again associative array to fetch all records relevant to the first associative array hope it will decrease a little bit time. thankx

answered Oct 15, 2014 at 8:57

[PHP 4, PHP 5, PHP 7, PHP 8]

set_time_limitLimits the maximum execution time

Description

set_time_limit[int $seconds]: bool

When called, set_time_limit[] restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit[20] is made, the script will run for a total of 45 seconds before timing out.

Parameters

seconds

The maximum execution time, in seconds. If set to zero, no time limit is imposed.

Return Values

Returns true on success, or false on failure.

Notes

Note:

The set_time_limit[] function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system[], stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

mba_aslam at yahoo dot com

15 years ago

while setting the set_time_limit[], the duration of sleep[] will be ignored in the execution time. The following illustrates:



Output:
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10

kexianbin at diyism dot com

8 years ago

Both set_time_limit[...] and  ini_set['max_execution_time',...]; won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec[], to run static method/function in background/detached process and time is out kill it:

my_exec.php:

jatin at jatinchimote dot com

16 years ago

If you set the number of seconds to a very large number [not many ppl do that, but just in case] then php exits with a fatal error like :

Fatal error: Maximum execution time of 1 second exceeded in /path/to/your/script/why.php

[EDIT by danbrown AT php DOT net: This is due to the limit of 32-bit signed integers.]

ravenswd at gmail dot com

13 years ago

Unfortunately, a script which gets into an infinite loop can produce an alarming amount of output in only a few seconds. I was attempting to debug a script, and I added



to the beginning of the script. Unfortunately, even two seconds of run time produced enough output to overload the memory available to my browser.

So, I wrote a short routine which would limit the execution time, and also limit the amount of output returned. I added this to the beginning of my script and it worked perfectly:

agvozden at gmail dot com

11 years ago

If you got something like:

msg: set_time_limit[] [function.set-time-limit]: Cannot set time limit in safe mode

try this:

bjfieldNO at SPAMgmail dot com

15 years ago

Timeouts after five minutes in IIS on Windows are caused by an inherited CGI Timeout value of 300 seconds.  This is not a PHP problem.  The fix is to add custom values for the files or directories that need longer to run.

In IIS 5.0 or 7.0 [beta as of this note], you can change this value on a fairly granular level using IIS Manager, under [roughly] YOURSITE -> Properties -> Home Directory -> Configuration [button] -> Options, but in IIS 6.0, this functionality is turned off [!], so you have to get into the Metabase.

Find the site number in Metabase Explorer [e.g., 12345678], then from CMD prompt:

[get to the scripts dir]
cd C:\Inetpub\AdminScripts

[this for each subdirectory from off the site root]
cscript adsutil.vbs CREATE W3SVC/12345678/root/"MY SUBDIRECTORY" IIsWebDirectory

[this for the file in question]
cscript adsutil.vbs CREATE W3SVC/12345678/root/"MY SUBDIRECTORY"/ILikeToTimeOut.php IIsWebFile

[this to set the timeout]
cscript adsutil.vbs set W3SVC/12345678/root/"MY SUBDIRECTORY"/ILikeToTimeOut.php/CGITimeout "7200"

Note:  "7200" is 2 hours in seconds, but can be whatever.

I derived the solution above from this fine article:
//www.iis-resources.com/modules/AMS/article.php?
storyid=509&page=3

Silver_Knight

12 years ago

if you are running a script that needs to execute for unknown time, or forever.. you may use
set_time_limit[0];
.....
...
..
.
and at the end of the script use flush[] function to tell phpto send out what it has generated.

Cleverduck

16 years ago

Regarding what 'nytshadow' said, it's important to realize that max-execution-time and the set_time_limit functions measure the time that the CPU is working on the script.  If the script blocks, IE: for input, select, sleep, etc., then the time between blocking and returning is NOT measured.  This is the same when running scripts from the command line interface.  So if you've got a log parser written in PHP that tails a file, that program WILL fail eventually.  It just depends how long it takes to read in enough input to process for 30 seconds.

If you're writing a command line script that should run infinitely, setting max-execution-time to 0 [never stop] is HIGHLY recommended.

BW

13 years ago

If you use Apache you can change maximum execution time by .htaccess with this line

php_value max_execution_time 200

mingalevme at gmail dot com

10 years ago

If you're using PHP_CLI SAPI and getting error "Maximum execution time of N seconds exceeded" where N is an integer value, try to call set_time_limit[0] every M seconds or every iteration. For example:

How can we increase the execution time of a PHP script?

Set Max_Execution_Time globally in php..
Locate and open your PHP build folder..
Find the php.ini file and open it..
Find the following line in the text configuration file – max_execution_time=30..
Change the value 30 to the value of choice, Remember, this value is in seconds..
Save & Close..

What is Max execution time PHP?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.

How do I fix PHP timeout?

Set the max_execution_time in your PHP configuration file to the number of seconds that want to allow your PHP scripts to execute..
Open php. ini file using your favourite text editor. ... .
Set the value for max_execution_time in seconds. max_execution_time = 300. ... .
Restart your web server. $ sudo systemctl restart apache2..

Does PHP have a timeout?

The default timeout is 30 seconds. It can be changed using the max_execution_time php.

Chủ Đề