Skip to content

Instantly share code, notes, and snippets.

@twlz0ne
Created March 11, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twlz0ne/bc8b4f11c1e7b13779f77dc5f42070e3 to your computer and use it in GitHub Desktop.
Save twlz0ne/bc8b4f11c1e7b13779f77dc5f42070e3 to your computer and use it in GitHub Desktop.
benchmark native and json.el #emacs
;;; Usage: /path/to/emacs -Q --batch -l /path/to/benchmark-native-json.el
;;; Date: 2019-03-10_15.53.34
(require 'json)
(defconst json-string
;; @from https://github.com/syohex/emacs-parson/blob/master/bench/bench.json
"{
\"glossary\": {
\"title\": \"example glossary\",
\"GlossDiv\": {
\"title\": \"S\",
\"GlossList\": {
\"GlossEntry\": {
\"ID\": \"SGML\",
\"SortAs\": \"SGML\",
\"GlossTerm\": \"Standard Generalized Markup Language\",
\"Acronym\": \"SGML\",
\"Abbrev\": \"ISO 8879:1986\",
\"GlossDef\": {
\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",
\"GlossSeeAlso\": [
\"GML\",
\"XML\"
]
},
\"GlossSee\": \"markup\"
}
}
}
}
}")
(defmacro run-benchmark (exp)
`(message "%-66S ;; => %s"
',exp
(let ((inhibit-message t))
(with-temp-buffer
,exp))))
(message (replace-regexp-in-string "\n" "" (emacs-version)))
(run-benchmark (benchmark '100000 '(json-serialize nil)))
(run-benchmark (benchmark '100000 '(json-encode nil)))
(run-benchmark (benchmark '100000 '(json-serialize [1 2 3 4 5])))
(run-benchmark (benchmark '100000 '(json-encode [1 2 3 4 5])))
(run-benchmark (benchmark '100000 '(json-parse-string "[true]")))
(run-benchmark (benchmark '100000 '(json-read-from-string "[true]")))
(run-benchmark (benchmark '100000 '(json-parse-string "{}")))
(run-benchmark (benchmark '100000 '(json-read-from-string "{}")))
(run-benchmark (benchmark '100000 '(json-parse-string json-string)))
(run-benchmark (benchmark '100000 '(json-read-from-string json-string)))
;;; benchmark-native-json.el ends here
@xuchunyang
Copy link

~/s/emacs $ src/emacs -Q --batch -l benchmark-native-json.el
GNU Emacs 28.0.50 (build 2, x86_64-apple-darwin18.7.0, NS appkit-1671.60 Version 10.14.6 (Build 18G1012)) of 2020-01-01
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 0.268941s (0.216736s in 15 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.122258s (0.208184s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 0.400901s (0.224517s in 15 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 1.883898s (1.276191s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.220715s (0.236130s in 16 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 1.983789s (1.286500s in 85 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 0.590364s (0.561388s in 39 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 2.003027s (1.374641s in 81 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 11.416063s (7.924459s in 496 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 19.642221s (13.262667s in 866 GCs)

It seems json.c was improved in recents months.

https://github.com/emacs-mirror/emacs/commits/master/src/json.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment