Replacing CreateTimeSpan()

Share this article

…posted by davidjmedlock:

I was reading up on some ColdFusion optimization techniques recently for work and I came across something that said you could increase the efficiency of caching your queries by eliminating the CreateTimeSpan() function used in the CachedWithin attribute of your cfquery tags.

Now, here’s my disclaimer: This is not really my idea. I saw it last week and for the life of me I can’t find it anywhere and I can’t remember where it was. (Seems like it was House of Fusion, but I didn’t see it in there unless I just missed it.) To the author who came up with it, if you read this let me know so I can give due credit. It’s pretty cool and I thought I’d share with our audience here.

CreateTimeSpan simply takes the days, hours, minutes, and seconds and converts them to a decimal number. This is based on days. So one day is 1, 1 day and 1 hour is 1.0416666666667 and so on. So, you can eliminate the function call to CreateTimeSpan() by doing a little junior high math on your own:

1 day: 1

1 hour: 0.0416666666667

1 minute: 0.000694444444444

1 second: 1.15740740741

If you want to figure out various fractions yourself, you just have to do this:

Hours: (h / 24) = timespan

Minutes (m / 24) / 60 = timespan

Seconds = (s / 24) / 60 / 60 = timespan

That will give you a number you can drop into cachedWithin to keep from having that additional function call. Whether this makes a noticeable difference, I can’t say. If you have a lot of cached queries, it just might, especially if they’re refreshed frequently. As with any optimization techniques, you’ll really start to notice the difference as your application grows, even though you may not see it in its infancy.

To give credit where credit is due, this was actually on House of Fusion as I thought. It was actually on the one place I didn’t look: the front page. Under Optimization Overkill you’ll find more information. Thanks to author Michael Dinowitz for pointing it out.

View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week