Skip to content

Instantly share code, notes, and snippets.

@tbatchelli
Forked from weavejester/hiccup-benchmark.clj
Created March 17, 2010 00:00
Show Gist options
  • Save tbatchelli/334692 to your computer and use it in GitHub Desktop.
Save tbatchelli/334692 to your computer and use it in GitHub Desktop.
(ns benchmarks
(:use net.cgrand.enlive-html)
(:require [hiccup.core :as hiccup]
[clj-html.core :as clj-html]))
(defn clj-html-benchmark []
(let [text "Some text"]
(clj-html/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example text]
[:ul.times-table
(for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]]])))
(defn hiccup-benchmark []
(let [text "Some text"]
(hiccup/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example text]
[:ul.times-table
(for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]]])))
(defn hint-hiccup-benchmark []
(let [text "Some text"]
(hiccup/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example #^String text]
[:ul.times-table
(for [n (range 1 13)]
[:li #^Number n " * 9 = " (* #^Number n 9)])]]])))
(defn str-benchmark []
(let [text "Some text"]
(str "<html><head><title>Literal String</title</head>"
"<body><div class=\"example\">" text "</div>"
"<ul class=\"times-table\">"
(apply str
(for [n (range 1 13)]
(str "<li>" n " * 9 = " (* n 9) "</li>")))
"</ul></body></html>")))
(deftemplate test-template
"template.html"
[]
[:ul.times-table :li] (clone-for [n (range 1 13)]
#(at % [:li]
(content (str n " * 9 = " (* 9 n))))))
(defn enlive-benchmark []
(apply str (test-template)))
(defsnippet test-snippet
"template.html"
[:ul.times-table]
[n]
[:li] (content (str n " * 9 = " (* 9 n))))
(deftemplate test-template-with-snippet
"viewbenchmarks/template.html"
[]
[:ul.times-table] (content (map test-snippet (range 1 13))))
(defn enlive-snippet-benchmark []
(apply str (test-template-with-snippet)))
(defn run-benchmark [f]
(dotimes [_ 3]
(time (dotimes [_ 100000] (f)))))
(println "clj-html")
(run-benchmark clj-html-benchmark)
(println "hiccup")
(run-benchmark hiccup-benchmark)
(println "hiccup (type-hint)")
(run-benchmark hint-hiccup-benchmark)
(println "str")
(run-benchmark str-benchmark)
(println "enlive")
(run-benchmark enlive-benchmark)
(println "enlive with snippet")
(run-benchmark enlive-snippet-benchmark)
clj-html
"Elapsed time: 8217.828 msecs"
"Elapsed time: 7400.953 msecs"
"Elapsed time: 7376.69 msecs"
hiccup
"Elapsed time: 2365.78 msecs"
"Elapsed time: 2261.179 msecs"
"Elapsed time: 2228.927 msecs"
hiccup (type-hint)
"Elapsed time: 1979.135 msecs"
"Elapsed time: 1880.187 msecs"
"Elapsed time: 1877.943 msecs"
str
"Elapsed time: 1864.898 msecs"
"Elapsed time: 1784.201 msecs"
"Elapsed time: 1771.764 msecs"
enlive
"Elapsed time: 91530.114 msecs"
"Elapsed time: 89804.175 msecs"
"Elapsed time: 90351.08 msecs"
enlive with snippet
"Elapsed time: 168646.418 msecs"
"Elapsed time: 169400.119 msecs"
"Elapsed time: 168887.373 msecs"
<html>
<head>
<title>Literal String</title>
</head>
<body>
<div class="example">text</div>
<ul class="times-table">
<li></li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment