Skip to content

Instantly share code, notes, and snippets.

@javan
Created October 2, 2008 21:10
Show Gist options
  • Select an option

  • Save javan/14443 to your computer and use it in GitHub Desktop.

Select an option

Save javan/14443 to your computer and use it in GitHub Desktop.
# UPDATE: I benchmarked parsing JSON and it's way way faster. I'm going to transition to requesting
# JSON from flickr and store the cached data as JSON too.
#
# With Rreset (http://github.com/javan/rreset/tree/master), I am caching some of the XML
# returned from Flickr API calls. Then, on subsequent hits, the cached XML is loaded and parsed
# using Hash.from_xml which is an implementation of XmlSimple. I was curious if it would be
# faster to first convert the XML to Yaml and parse that on cached hits instead. Yaml was the
# obvious winner. There are probably way faster XML parsing libraries out there,
# but I wanted to stick with the Rails defaults.
#
# The results:
#
# Hash.from_xml 25.760000 0.100000 25.860000 ( 26.154997)
# YAML::load 1.080000 0.010000 1.090000 ( 1.089976)
# JSON.parse 0.300000 0.000000 0.300000 ( 0.305692)
require 'rubygems'
require 'activesupport'
require 'benchmark'
require 'yaml'
require 'json'
xml = <<-XML
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photo id="2827050884" secret="68ba1e9bcb" server="3279" farm="4" dateuploaded="1220497642" isfavorite="0" license="1" rotation="0" originalsecret="0101206da5" originalformat="jpg" media="photo">
<owner nsid="51035706609@N01" username="javan" realname="Javan Makhmali" location="Portland, Oregon" />
<title>Gwenaëlle's new lifestyle</title>
<description />
<visibility ispublic="1" isfriend="0" isfamily="0" />
<dates posted="1220497642" taken="2008-09-03 20:07:22" takengranularity="0" lastupdate="1220505162" />
<editability cancomment="0" canaddmeta="0" />
<usage candownload="1" canblog="0" canprint="0" />
<comments>1</comments>
<notes />
<tags />
<urls>
<url type="photopage">http://www.flickr.com/photos/javan/2827050884/</url>
</urls>
</photo>
</rsp>
XML
yaml = <<-YAML
---
rsp:
photo:
visibility:
ispublic: "1"
isfamily: "0"
isfriend: "0"
usage:
candownload: "1"
canprint: "0"
canblog: "0"
rotation: "0"
license: "1"
comments: "1"
title: "Gwena\xC3\xABlle's new lifestyle"
tags:
dateuploaded: "1220497642"
urls:
url: http://www.flickr.com/photos/javan/2827050884/
editability:
canaddmeta: "0"
cancomment: "0"
media: photo
farm: "4"
notes:
dates:
taken: 2008-09-03 20:07:22
takengranularity: "0"
lastupdate: "1220505162"
posted: "1220497642"
originalsecret: 0101206da5
id: "2827050884"
description:
isfavorite: "0"
server: "3279"
owner:
nsid: 51035706609@N01
username: javan
realname: Javan Makhmali
location: Portland, Oregon
originalformat: jpg
secret: 68ba1e9bcb
stat: ok
YAML
json = <<-JSON
{"rsp": {"photo": {"visibility": {"ispublic": "1", "isfamily": "0", "isfriend": "0"}, "usage": {"candownload": "1", "canprint": "0", "canblog": "0"}, "rotation": "0", "license": "1", "comments": "1", "title": "Gwena\u00eblle's new lifestyle", "tags": null, "dateuploaded": "1220497642", "urls": {"url": "http://www.flickr.com/photos/javan/2827050884/"}, "editability": {"canaddmeta": "0", "cancomment": "0"}, "media": "photo", "farm": "4", "notes": null, "dates": {"taken": "2008-09-03 20:07:22", "takengranularity": "0", "lastupdate": "1220505162", "posted": "1220497642"}, "originalsecret": "0101206da5", "id": "2827050884", "description": null, "isfavorite": "0", "server": "3279", "owner": {"nsid": "51035706609@N01", "username": "javan", "realname": "Javan Makhmali", "location": "Portland, Oregon"}, "originalformat": "jpg", "secret": "68ba1e9bcb"}, "stat": "ok"}}
JSON
n = 5000
Benchmark.bm(15) do |x|
x.report("Hash.from_xml") { n.times { Hash.from_xml(xml) } }
x.report("YAML::load") { n.times { YAML::load(yaml) } }
x.report("JSON.parse") { n.times { JSON.parse(json) } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment