rfc822() vs. rfc1123_date()

16 August 2007   0 comments   Zope

Mind That Age!

This blog post is 11 years old! Most likely, its content is outdated. Especially if it's technical.

To set the Expires header in my web application I used to use the Zope DateTime function rfc822() which doesn't return the date in GMT format. Here's how I used it:

>>> from DateTime 
import DateTime
>>> hours = 5
>>> then = DateTime() + hours/24.0
>>> then.rfc822()
'Thu, 16 Aug 2007 20:43:59 +0100'

Then I found out (from using YSlow) that it's better to use the GMT format (RFC 1123), and here's how to do that in Zope:

>>> from App.Common import rfc1123_date
>>> from time import time
>>> rfc1123_date(time() + 3600*hours)
'Thu, 16 Aug 2007 19:45:12 GMT'

(notice that even though my locale is here in London, because of the summer time an hour is added)

Well, I thought I'd do a quick benchmark to compare the two approaches because I suspected that rfc1123_date() was faster because you don't have to create a DateTime() object. Here's what the two methods looked like to benchmark it:

   def foo1(self, hours):
       t0 = time()
       now = DateTime()
       then = now+float(hours/24.0)
       x=then.rfc822()
       return time()-t0

   def foo2(self, hours):
       t0 = time()
       x = rfc1123_date(time() + 3600*hours)
       return time()-t0

The result was as I expected, rfc1123_date() was much faster. Here are the results for 10,000 iterations:

Times1: 1.25969386101
Times2: 0.16867017746

round(1.25969386101/0.16867017746)=7.0

But a benchmark on something like this is a bit non-sense. Why? Because even if there's a 7 times difference, you'll only ever need one of these iterations per page. Not 10,000. The first function foo1() takes 0.00013 seconds.

Conclusion, worry more about getting the right output rather than speed at this kind of level.

Comments

Your email will never ever be published


Related posts

Previous:
html2plaintext Python script to convert HTML emails to plain text 10 August 2007
Next:
Printer usability problem 24 August 2007
Related by Keyword:
The ideal number of workers in Jest 08 October 2018
Fastest Python datetime parser 02 May 2018
Best EXPLAIN ANALYZE benchmark script 19 April 2018
Fastest *local* cache backend possible for Django 04 August 2017
How to do performance micro benchmarks in Python 24 June 2017
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
Ever wondered how much $87 Billion is? 04 November 2003
Guake, not Yakuake or Yeahconsole 23 January 2010