cyborgzombieninjapirate


Let me tell you about this slow date() I had.

Posted on 21.09.2009 12:27 am

I hadn't fired up the profiler on the blog code for a while. So I thought I'd venture into the cruel world of cachegrind for an evening.

My goal was to monitor changes in one part of the code and see what would happen in terms of processor time.

But I never got to that. Because after the first run of the profiler. I got a shock.

date() was using 31% of the processing time (2.9µ on a 9.572µs process) of the front page, and it's only running twice! My jaw dropped to the flow. 31% and it runs twice ?! The template parsing function uses 14% and it runs 35 times. That's 0.4% per execution vs 15.5%

When I ran the profiler on the archive page (where date runs 17 times) I got this. (44.9% for 17 executions)

I knew that date() is tough on locale settings and default timezones. So if I had those settings wrong, then it could be throwing notices. But I have the error setting to the highest and there was a default timezone set in the php.ini file.

So what is a girl to do when her date is acting slow?

There are other functions for formatting date. Like mktime(), gmdate() and strftime().

gmdate() is basically date except it returns formatted GMT dates.

mktime() is the other way around, you input a human readable date format and it returns a unix timestamp. What I have are timestamps and I need pretty output. So that's a no.

strftime() is a neat thing. It formats a date like date() does but by locale settings. It uses a slightly different format but it has all the same options as date() and more.

So I tried strftime(). Ran the profiler again and opened up the output document.

And I scrolled... and I scrolled.... oh there it is.

strftime() clocked in at 1.24% for two executions. That's 0.62% per execution. (0.2µs on 6.3µs)

Looking good, another run with the profiler confirms this. I even ran this on the archive page. Where it ran 17 times at 3.22%. So it even scales well.

I'm not 100% on this though. Since I got a few profiler runs that clocked date at a much lower rate (about 0.2% per execution). But hopefully time will tell.

0 8    Like it or hate it?  -  Comment (0)


(Used for gravatar only, never displayed)

What is 3 + 5


Memory allocated for your request: 181.84 Kb
Process time: 0.011539 seconds