Guest User

Untitled

a guest
May 20th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. (ns benchmarks
  2. (:use net.cgrand.enlive-html)
  3. (:require [hiccup.core :as hiccup]
  4. [clj-html.core :as clj-html]))
  5.  
  6. (defn clj-html-benchmark []
  7. (let [text "Some text"]
  8. (clj-html/html
  9. [:html
  10. [:head
  11. [:title "Literal String"]]
  12. [:body
  13. [:div.example text]
  14. [:ul.times-table
  15. (for [n (range 1 13)]
  16. [:li n " * 9 = " (* n 9)])]]])))
  17.  
  18. (defn hiccup-benchmark []
  19. (let [text "Some text"]
  20. (hiccup/html
  21. [:html
  22. [:head
  23. [:title "Literal String"]]
  24. [:body
  25. [:div.example text]
  26. [:ul.times-table
  27. (for [n (range 1 13)]
  28. [:li n " * 9 = " (* n 9)])]]])))
  29.  
  30. (defn hint-hiccup-benchmark []
  31. (let [text "Some text"]
  32. (hiccup/html
  33. [:html
  34. [:head
  35. [:title "Literal String"]]
  36. [:body
  37. [:div.example #^String text]
  38. [:ul.times-table
  39. (for [n (range 1 13)]
  40. [:li #^Number n " * 9 = " (* #^Number n 9)])]]])))
  41.  
  42. (defn str-benchmark []
  43. (let [text "Some text"]
  44. (str "<html><head><title>Literal String</title</head>"
  45. "<body><div class=\"example\">" text "</div>"
  46. "<ul class=\"times-table\">"
  47. (apply str
  48. (for [n (range 1 13)]
  49. (str "<li>" n " * 9 = " (* n 9) "</li>")))
  50. "</ul></body></html>")))
  51.  
  52. (deftemplate test-template
  53. "template.html"
  54. []
  55. [:ul.times-table :li] (clone-for [n (range 1 13)]
  56. #(at % [:li]
  57. (content (str n " * 9 = " (* 9 n))))))
  58.  
  59.  
  60. (defn enlive-benchmark []
  61. (apply str (test-template)))
  62.  
  63. (defsnippet test-snippet
  64. "template.html"
  65. [:ul.times-table]
  66. [n]
  67. [:li] (content (str n " * 9 = " (* 9 n))))
  68.  
  69. (deftemplate test-template-with-snippet
  70. "viewbenchmarks/template.html"
  71. []
  72. [:ul.times-table] (content (map test-snippet (range 1 13))))
  73.  
  74. (defn enlive-snippet-benchmark []
  75. (apply str (test-template-with-snippet)))
  76.  
  77. (defn run-benchmark [f]
  78. (dotimes [_ 3]
  79. (time (dotimes [_ 100000] (f)))))
  80.  
  81. (println "clj-html")
  82. (run-benchmark clj-html-benchmark)
  83.  
  84. (println "hiccup")
  85. (run-benchmark hiccup-benchmark)
  86.  
  87. (println "hiccup (type-hint)")
  88. (run-benchmark hint-hiccup-benchmark)
  89.  
  90. (println "str")
  91. (run-benchmark str-benchmark)
  92.  
  93. (println "enlive")
  94. (run-benchmark enlive-benchmark)
  95.  
  96. (println "enlive with snippet")
  97. (run-benchmark enlive-snippet-benchmark)
Add Comment
Please, Sign In to add comment