How much execution time is left?  
Author Message
Mikhail Kovalev





PostPosted: 2008-2-9 5:22:20 Top

php-general, How much execution time is left? I'm using set_time_limit() to set maximum execution time. Is there a
way to check how much time is left at any time during the execution
itself?
 
thib





PostPosted: 2008-2-9 9:33:00 Top

php-general >> How much execution time is left? Mikhail Kovalev wrote:
> I'm using set_time_limit() to set maximum execution time. Is there a
> way to check how much time is left at any time during the execution
> itself?

Use time() or microtime() at the beginning of the script and store the
value. Later, call it again and substract the start time, you'll get the
elapsed time. Another substraction of the time limit by the elapsed time
will give you the remaining time.

-thib?
 
Jerry Stuckle





PostPosted: 2008-2-9 11:21:00 Top

php-general >> How much execution time is left? thib?wrote:
> Mikhail Kovalev wrote:
>> I'm using set_time_limit() to set maximum execution time. Is there a
>> way to check how much time is left at any time during the execution
>> itself?
>
> Use time() or microtime() at the beginning of the script and store the
> value. Later, call it again and substract the start time, you'll get the
> elapsed time. Another substraction of the time limit by the elapsed time
> will give you the remaining time.
>
> -thib? >

Not true.

set_time_limit is the limit for execution time. time() or microtime()
is real time. A huge difference.

For instance - say you have a time limit of 30 seconds. One second into
your script, a high priority process gets invoked and runs for two minutes.

You still have about 29 seconds left left on your script, but the time()
or microtime() function says you're 1 minute, 31 seconds over.

An extreme example, I know. But it gets the point across.

So no, there is no way to tell how much time is left.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
email***@***.com
==================

 
 
Michael Fesser





PostPosted: 2008-2-9 23:42:00 Top

php-general >> How much execution time is left? .oO(thib?

>Mikhail Kovalev wrote:
>> I'm using set_time_limit() to set maximum execution time. Is there a
>> way to check how much time is left at any time during the execution
>> itself?
>
>Use time() or microtime() at the beginning of the script and store the
>value. Later, call it again and substract the start time, you'll get the
>elapsed time. Another substraction of the time limit by the elapsed time
>will give you the remaining time.

This won't work. In addition to Jerry's example a quote from the manual:

| 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.

Micha
 
 
Rob





PostPosted: 2008-2-11 21:09:00 Top

php-general >> How much execution time is left? On Feb 9, 3:42爌m, Michael Fesser <email***@***.com> wrote:
> .oO(thib?
>
> >Mikhail Kovalev wrote:
> >> I'm using set_time_limit() to set maximum execution time. Is there a
> >> way to check how much time is left at any time during the execution
> >> itself?
>
> >Use time() or microtime() at the beginning of the script and store the
> >value. Later, call it again and substract the start time, you'll get the
> >elapsed time. Another substraction of the time limit by the elapsed time
> >will give you the remaining time.
>
> This won't work. In addition to Jerry's example a quote from the manual:
>
> | 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.
>
> Micha

True, but in general terms, Thib's solution should work - unless
Mikhail knows beforehand that there will be a lot of non-PHP
processing going on, i.e. file transfers.

Rob.
 
 
thib





PostPosted: 2008-2-14 5:34:00 Top

php-general >> How much execution time is left? Michael Fesser wrote:
> .oO(thib?
>
>> Mikhail Kovalev wrote:
>>> I'm using set_time_limit() to set maximum execution time. Is there a
>>> way to check how much time is left at any time during the execution
>>> itself?
>> Use time() or microtime() at the beginning of the script and store the
>> value. Later, call it again and substract the start time, you'll get the
>> elapsed time. Another substraction of the time limit by the elapsed time
>> will give you the remaining time.
>
> This won't work. In addition to Jerry's example a quote from the manual:
>
> | 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.
>
> Micha

Good to know, thanks.

So, a not absolutely reliable way would be to stop the counter before every
'heavy' instruction and restart it again just after it, right?
I've seen some tiny handy "stopwatch" functions that could make the job
without too much complications.

Anyway, is it really worth it, Mikhail?

-thib?
 
 
bio-anomoly





PostPosted: 2008-2-19 12:15:00 Top

php-general >> How much execution time is left? There is a system resource call where PHP will admit how much system
time and user time has been used.

If you use lots of sleeps, freads, and cURLs, etc, you use very little
system time.

I have executed scripts on Godaddy websites [default CPU time 30
seconds] which are heavily waited, but the scripts are still
terminated after 15-20 minutes. I guess there is a dead-mans-handle.

 
 
bio-anomoly





PostPosted: 2008-2-19 12:28:00 Top

php-general >> How much execution time is left? Gets the current resource usages (PHP 3>= 3.0.7, PHP 4 )
array getrusage ( [int who] )

This is an interface to getrusage(2). It returns an associative array
containing the data returned from the system call. If who is 1,
getrusage will be called with RUSAGE_CHILDREN.

All entries are accessible by using their documented field names.

Example 724. getrusage() example
copy to clipboard
<?php
$dat = getrusage();
echo $dat["ru_nswap"]; # number of swaps
echo $dat["ru_majflt"]; # number of page faults
echo $dat["ru_utime.tv_sec"]; # user time used (seconds)
echo $dat["ru_utime.tv_usec"]; # user time used (microseconds)
?>

Also posix_times()
 
 
bio-anomoly





PostPosted: 2008-2-19 12:37:00 Top

php-general >> How much execution time is left? http://www.ds-o.com/archives/27-Benchmarking-misconceptions,-microtime-vs.-getrusage.html

getrusage() is not fully documented in the PHP manual. Linux platforms
only.

posix_times may work on Windows. (It doesn't say otherwise).

ru_utime.tv_sec, ru_utime.tv_usec: the total amount of time spent
executing in user mode.
ru_stime.tv_sec, ru_stime.tv_usec: the total amount of time spent in
the system executing on behalf of the process(es).
ru_maxrss: the maximum resident set size utilized (in kilobytes).
ru_ixrss: an integral value indicating the amount of memory used by
the text segment that was also shared among other processes. This
value is expressed in units of kilobytes * ticks-of-execution.
ru_idrss: an integral value of the amount of unshared memory residing
in the data segment of a process (expressed in units of kilobytes *
ticks-of-execution).
ru_isrss: an integral value of the amount of unshared memory residing
in the stack segment of a process (expressed in units of kilobytes *
ticks-of-execution).
ru_minflt: the number of page faults serviced without any I/O
activity; here I/O activity is avoided by reclaiming a page frame from
the list of pages awaiting reallocation.
ru_majflt: the number of page faults serviced that required I/O
activity.
ru_nswap: the number of times a process was swapped out of main
memory.
ru_inblock: the number of times the file system had to perform input.
ru_oublock: the number of times the file system had to perform
output.
ru_msgsnd: the number of IPC messages sent.
ru_msgrcv: the number of IPC messages received.
ru_nsignals: the number of signals delivered.
ru_nvcsw: the number of times a context switch resulted due to a
process voluntarily giving up the processor before its time slice was
completed (usually to await availability of a resource).
ru_nivcsw: the number of times a context switch resulted due to a
higher priority process becoming runnable or because the current
process exceeded its time slice.
The values we are worried about for giving a benchmark would be the
ru_utime.* and ru_stime.* variables. The example I am going to show
you will help you benchmark the system time used. If you would like to
captuer the user time then it is just a matter of variable swapping.