views:

68

answers:

3

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?

A: 

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.

Ricky
@Ricky. I agree with you, except for the `Run a ridiculous number of values through a loop' part. You should run a *reasonable* number of values according to your app requirements and expectations. The whole point is knowing if using one method will seriously affect your app's performance, not knowing which one is faster in absolute terms.
Juan Pablo Califano
A: 

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.

Juan Pablo Califano
I have kcachegrind and xdebug outputting grinds. I just hoped to avoid setting up an entire profiling sandpit, by asking if someone already did this and *knows* it is either very slow or very fast. `a good programmer is a lazy programmer`. I try hard to be a good programmer :)
berkes
@berkes. Yeah, I know, and it's wise to ask, maybe someone does know that. But at any rate, I'd insist in the importance of considering any of these figures in the context of your actual app. And you are the only one who knows that ;).
Juan Pablo Califano
@berkes: it will always depend on context. A good programmer is not a lazy programmer. A good programmer knows where to effectively budget their time. And worrying about optimizing a routine without knowing if it will be beneficial to optimize it is wasting time. Remember that on the order of 95% of your performance problems will be related to 5% of your code. Fix the 5% and you'll get the most gains. But finding out which 5% requires profiling is the key... Don't assume think that because one routine **might** be expensive that it will cause you problems.
ircmaxell
+1  A: 

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

ircmaxell