PHP strtotime() 的性能如何?

发布于 2024-09-27 10:42:28 字数 1060 浏览 6 评论 0原文

我正在进行一些大型时间戳列表迭代:将它们放入 带有日期范围的表格,并按范围对它们进行分组。 为了做到这一点,我发现 strtotime() 是一个非常有用的函数,但我担心它的性能

例如,一个循环遍历周列表(例如,第 49 周到第 05 周)的函数,并且必须确定该周的开始和该周结束时的时间戳。一种有用的方法是:

foreach ($this->weeks($first, $amount) as $starts_at) {
  $ends_at = strtotime('+1 week', $starts_at);
  $groups[$week_key] = $this->slice($timestamps, $starts_at, $ends_at);
}

//$this->weeks returns a list of timestamps at which each week starts.
//$this->slice is a simple helper that returns only the timestamps within a range, from a list of timestamps. 

我可以找出一周开始和结束之间的秒数,而不是 strtotime(),99% 的时间是24 * 60 * 60 * 7。但在这些存在 DST 切换的罕见情况下,24 应该是 23 或 25。解决这个问题的代码可能会比 strtotime() 慢很多,不是吗?

我对年、月(月,非常不一致!)、日和小时的范围使用相同的模式。只有几个小时我才会怀疑简单地将 3600 添加到时间戳会更快。

还有其他问题吗?是否有方法(不依赖于 PHP5.3!)为一致、DST 和闰年安全日期范围提供更好的路线?

I am doing some large timestamp-list iterations: Putting them in tables with date-ranges, and grouping them by ranges.
In order to do that, I found strtotime() a very helpfull function, but I am worried about its performance.

For example, a function that loops over a list of weeks (say, week 49 to 05) and has to decide the beginning of the week and the timestamp at the end of that week. A usefull way to do that, would be:

foreach ($this->weeks($first, $amount) as $starts_at) {
  $ends_at = strtotime('+1 week', $starts_at);
  $groups[$week_key] = $this->slice($timestamps, $starts_at, $ends_at);
}

//$this->weeks returns a list of timestamps at which each week starts.
//$this->slice is a simple helper that returns only the timestamps within a range, from a list of timestamps. 

Instead of strtotime(), I could, potentially, find out the amount of seconds between begin and end of the week, 99% of the times that would be 24 * 60 * 60 * 7. But in these rare cases where there is a DST-switch, that 24 should either be 23 or 25. Code to sort that out, will probably be a lot slower then strtotime(), not?

I use the same patterns for ranges of years, months (months, being very inconsistent!), days and hours. Only with hours would I suspect simply adding 3600 to the timestamp is faster.

Any other gotcha's? Are there ways (that do not depend on PHP5.3!) that offer better routes for consistent, DST and leap-year safe dateranges?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-10-04 10:42:29

非常有趣的问题。我想说,真正解决这个问题的唯一方法是设置自己的性能测试。观察脚本开头和结尾处 microtime() 的值,以确定性能。使用一种方法,然后使用另一种方法,通过循环运行大量的值。比较次数。

Very interesting question. I'd say that the only way you can really figure this out, is to set up your own performance test. Observe the value of microtime() at the beginning and end of the script, to determine performance. Run a ridiculous number of values through a loop with one method, then the other method. Compare times.

楠木可依 2024-10-04 10:42:28

您为什么担心它的表现?您有证据表明它正在减慢您的系统吗?如果不是,请不要出于不必要的原因尝试使解决方案过于复杂。请记住,过早的优化是万恶之源。编写有意义的可读代码,并且仅在您知道这将是一个问题时才进行优化...

但需要考虑的其他事情是它也是编译的 C 代码,因此它的功能应该非常高效。您也许能够在 PHP 领域构建代码的子集并使其更快,但这将是一项困难的工作(由于 PHP 代码涉及所有开销)。

就像我之前说的,使用它直到证明它是一个问题,然后解决该问题。不要忘记根据您的需要重写它也不是免费的。这需要时间并引入错误。如果增益很小(这意味着它一开始就不是性能问题),那么值得吗?因此,除非您知道这是一个问题,否则不要费心尝试微观优化......

Why are you worried about its performance? Do you have evidence that it's slowing down your system? If not, don't try to over-complicate the solution for unnecessary reasons. Remember that premature optimization is the root of all evil. Write readable code that makes sense, and only optimize if you KNOW it's going to be an issue...

But something else to consider is that it's also compiled C code, so it should be quite efficient for what it does. You MIGHT be able to build a sub-set of the code in PHP land and make it faster, but it's going to be a difficult job (due to all the overhead involved in PHP code).

Like I said before, use it until you prove it's a problem, then fix that problem. Don't forget re-writing it for you needs isn't free either. It takes time and introduces bugs. Is it worth it if the gain is minimal (meaning it wasn't a performance problem to begin with). So don't bother trying to micro-optimize unless you KNOW it's a problem...

忆梦 2024-10-04 10:42:28

我知道这可能不是您正在寻找的答案,但您最好的选择是分析它并考虑真实用例

我的直觉是,正如您所想,strtotime 会更慢。但即使慢了 3 倍,这也只有在上下文中才有意义。也许您的例程(使用真实数据)使用 strtotime 需要 60 毫秒,因此在大多数情况下,您只会节省 40 毫秒(这些数字完全是我编造的,但您明白了)。因此,您可能会发现优化这一点并不会真正带来回报(考虑到您正在向更多潜在错误开放代码,并且您必须投入更多时间来解决问题)。

顺便说一句,如果您有良好的分析工具,那就太棒了,但即使您不比较时间戳也应该给您一个粗略的了解。

I know this probably isn't the answer you're looking for, but your best bet is profiling it with a real use case in mind.

My instinct is that, as you think, strtotime will be slower. But even if it's, say, 3 times slower, this is only meaningful in context. Maybe your routine, with real data, takes 60 ms using strtotime, so in most cases, you'd be just saving 40 ms (I totally made up these numbers, but you get the idea). So, you might find out that optimising this wouldn't really pay off (considering you're opening your code to more potential bugs and you'll have to invest more time to get it right).

By the way, if you have good profiling tools, awesome, but even if you don't comparing timestamps should give you a rough idea.

潜移默化 2024-10-04 10:42:28

最后要回答这个问题:

基于许多这样的基准: https://en.code-bude.net/2013/12/19/benchmark-strtotime-vs-datetime-vs-gettimestamp-in-php/

我们可以看到strtotime()比我们想象的更有效。
所以,是的,要将字符串转换为时间戳,strtotime 函数具有相当好的性能

To respond to the question, finaly :

Based on many benchmarks like this one: https://en.code-bude.net/2013/12/19/benchmark-strtotime-vs-datetime-vs-gettimestamp-in-php/

We can see that strtotime() is more effective that we can think.
So yes, to convert a string to a timestamp, the strtotime function has pretty good performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文