开发者

Calling time() multiple times bad?

I was just goin开发者_Go百科g through some code and was wondering if it is bad to call time() multiple times on a page?

Yes, I know that it will work when called repeatedly, but will there be a big speed difference?

Option #1: Call time() whenever you need the current Unix timestamp.

Option #2: Set a variable $timestamp = time(); and call the variable whenever the time is needed.


I'd expect time() to be reasonably cheap, but if you call it multiple times on the same page you could get some unexpected inconsistencies. For example, suppose you call it twice, once to display the date and once to display the time. If the date changes between the two calls, you could end up showing midnight of the previous day, or 23:59:59 of the next day (depending on the order of the calls).

I would normally expect the whole page to be treated as being rendered in a single instant, in terms of any times displayed - so call it once, and use that single value in multiple places.


That depends on the situation. Some scripts run for more than a few seconds, some could run for hours. Then the new value of time() would be wanted.

If a difference in time that is shorter than the time it takes for your php code to execute won't cause any problems then you might as well use Option #2 since it is slightly faster. But, the speed difference is negligible unless you make tons of calls to time().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜