# PHP 5.1 time() caching

- Thread: https://externals.io/message/13472
- Original archive: https://news-web.php.net/php.internals/13472
- Started by Ilia Alshanetsky on 2004-10-22T20:43:15+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13472 — Ilia Alshanetsky — 2004-10-22T20:43:15+00:00

URL: https://externals.io/message/13472#13472

As of PHP 5.1, the request start time is stored by PHP inside the sapi 
structure. This data is populated by the information offered by the SAPI 
(Apache sapis populate it) otherwise time(0) is used to get the same data.

This means that PHP has a "free" unix timestamp that tells information 
about the request start time, which does not require a syscall.

. Since a lot of script end up having to fetch request start time, this 
can be used to save on a timing call
(This information only has second precision, no microseconds).

The question is what would be the best way to provide this information 
within the script. The two alternatives are: adding a new function to 
get this info or storing this data inside $_SERVER.

What do you think?

Ilia

---

## #13473 — George Schlossnagle — 2004-10-22T20:50:14+00:00

URL: https://externals.io/message/13472#13473 · In reply to: #13472

>
> The question is what would be the best way to provide this information 
> within the script. The two alternatives are: adding a new function to 
> get this info or storing this data inside $_SERVER.
>
> What do you think?

$_SERVER['request_time']

George

---

## #13474 — dharana — 2004-10-22T20:57:07+00:00

URL: https://externals.io/message/13472#13474 · In reply to: #13472

Ilia Alshanetsky wrote:

> As of PHP 5.1, the request start time is stored by PHP inside the sapi 
> structure. This data is populated by the information offered by the SAPI 
> (Apache sapis populate it) otherwise time(0) is used to get the same data.
> 
> This means that PHP has a "free" unix timestamp that tells information 
> about the request start time, which does not require a syscall.
> 
> . Since a lot of script end up having to fetch request start time, this 
> can be used to save on a timing call
> (This information only has second precision, no microseconds).

I would think the reason for fetching request start time is to profile 
execution time (the first line you would see in my scripts is always 
$time_start = `microtime()`) or maybe it's just me, of course.

> 
> The question is what would be the best way to provide this information 
> within the script. The two alternatives are: adding a new function to 
> get this info or storing this data inside $_SERVER.

I'm for $_SERVER, it makes most sense for me.

> 
> What do you think?
> 
> Ilia
> 

-- 
Juan Alonso
http://gamersmafia.com | http://laflecha.net

---

## #13475 — Derick Rethans — 2004-10-22T21:00:38+00:00

URL: https://externals.io/message/13472#13475 · In reply to: #13474

> Ilia Alshanetsky wrote:
>
> > […]
>
> I would think the reason for fetching request start time is to profile
> execution time (the first line you would see in my scripts is always
> $time_start = `microtime()`) or maybe it's just me, of course.

For that the granularity is not good enough though.

> > […]

$_SERVER

Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #13476 — Ilia Alshanetsky — 2004-10-22T21:05:26+00:00

URL: https://externals.io/message/13472#13476 · In reply to: #13475

Derick Rethans wrote:
> For that the granularity is not good enough though.

Ideally we'd have `gettimeofday()` cached, but looking @ the code it is 
called on by lcg code and then only on MINIT. So we'd need an "extra" 
syscall to provide this, I am not sure this is a good idea.

Ilia

P.S. I didn't indicate in my earlier e-mail, I'd prefer $_SERVER as well.

---

## #13477 — George Schlossnagle — 2004-10-22T21:05:27+00:00

URL: https://externals.io/message/13472#13477 · In reply to: #13476

> Derick Rethans wrote:
> > […]
>
> Ideally we'd have `gettimeofday()` cached, but looking @ the code it is 
> called on by lcg code and then only on MINIT. So we'd need an "extra" 
> syscall to provide this, I am not sure this is a good idea.

I think this is available from Apache2, though not via the SAPI 
interface.

george

---

## #13480 — Andi Gutmans — 2004-10-22T23:35:18+00:00

URL: https://externals.io/message/13472#13480 · In reply to: #13472

At 10:57 PM 10/22/2004 +0200, dharana wrote:
>Ilia Alshanetsky wrote:
>
> > […]
>
>I would think the reason for fetching request start time is to profile 
>execution time (the first line you would see in my scripts is always 
>$time_start = `microtime()`) or maybe it's just me, of course.

I think using current time for timestamps in DBs and so on can be very 
useful. I think Ilia had in mind wider use and not specifically for 
profiling where the performance of the initial call to `time()` wouldn't have 
any significance.

> > […]
>
>I'm for $_SERVER, it makes most sense for me.

$_SERVER[] is fine. The downside that it's an extra `hash_update()` on each 
requests even if you don't use the time (as opposed to an internal 
function). But in real life this would not be noticeable.

Andi

---

## #13482 — Rasmus Lerdorf — 2004-10-23T01:50:01+00:00

URL: https://externals.io/message/13472#13482 · In reply to: #13476

> Derick Rethans wrote:
> > […]
>
> Ideally we'd have `gettimeofday()` cached, but looking @ the code it is
> called on by lcg code and then only on MINIT. So we'd need an "extra"
> syscall to provide this, I am not sure this is a good idea.
>
> Ilia
>
> P.S. I didn't indicate in my earlier e-mail, I'd prefer $_SERVER as well.

Sticking it in $_SERVER is ok, but then you also have to hack up `getenv()`
to recognize it and return it when asked.  People interested in using this
are likely people looking to avoid doing the extra syscall and these are
the same people who are unlikely to populate $_SERVER at all in their
configs.

-Rasmus
