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
@twlz0ne
Copy link
Author

twlz0ne commented Mar 11, 2019

> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 0.723119s (0.475642s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.141493s (0.272671s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 1.538631s (0.716513s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 3.283300s (1.849187s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.439487s (0.370970s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 4.360634s (2.339313s in 85 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 1.195033s (0.956900s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 4.176838s (2.517802s in 81 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 25.451464s (16.632284s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 30.148163s (18.175661s in 866 GCs)

⋊> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 0.843970s (0.542942s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.138864s (0.260691s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 0.892163s (0.483081s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 3.241392s (1.852060s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.411270s (0.342414s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 3.234683s (1.739951s in 85 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 0.994898s (0.871107s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 3.123997s (1.827660s in 81 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 22.577644s (14.639583s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 38.248179s (22.567974s in 866 GCs)
                                                                                                                                                                              
⋊> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
GNU Emacs 27.0.50 (build 7, x86_64-apple-darwin15.6.0, NS appkit-1404.47 Version 10.11.6 (Build 15G22010))
 of 2019-03-11
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 1.324073s (0.685536s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.258037s (0.352754s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 1.006789s (0.495774s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 6.093016s (2.595936s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.472901s (0.383466s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 4.191628s (2.199567s in 85 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 0.728043s (0.940686s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 3.047281s (1.782949s in 81 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 22.662395s (14.645539s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 29.729253s (17.643603s in 866 GCs)

⋊> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
GNU Emacs 27.0.50 (build 7, x86_64-apple-darwin15.6.0, NS appkit-1404.47 Version 10.11.6 (Build 15G22010)) of 2019-03-11
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 0.640114s (0.413379s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.150277s (0.255822s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 1.396987s (0.543863s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 4.602191s (2.348726s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.398737s (0.345628s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 3.383423s (1.781798s in 85 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 0.934046s (0.814709s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 3.268766s (1.839654s in 81 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 22.388149s (14.668245s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 29.638171s (17.242109s in 866 GCs)

⋊> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
GNU Emacs 27.0.50 (build 7, x86_64-apple-darwin15.6.0, NS appkit-1404.47 Version 10.11.6 (Build 15G22010)) of 2019-03-11
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 0.867231s (0.432982s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.166566s (0.288980s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 1.467265s (0.500945s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 3.830905s (1.712070s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.416691s (0.368764s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 3.211723s (1.631296s in 77 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 1.274721s (1.058400s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 2.714854s (1.509176s in 73 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 27.819788s (15.239697s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 30.269438s (18.141456s in 858 GCs)

⋊> [~/s/b/emacs-master] ./src/emacs -Q --batch -l ~/.scratch/emacs/benchmark-native-json.el
GNU Emacs 27.0.50 (build 7, x86_64-apple-darwin15.6.0, NS appkit-1404.47 Version 10.11.6 (Build 15G22010)) of 2019-03-11
(benchmark '100000 '(json-serialize nil))                          ;; => Elapsed time: 1.596267s (0.580090s in 22 GCs)
(benchmark '100000 '(json-encode nil))                             ;; => Elapsed time: 0.145475s (0.265138s in 14 GCs)
(benchmark '100000 '(json-serialize [1 2 3 4 5]))                  ;; => Elapsed time: 1.095452s (0.468233s in 24 GCs)
(benchmark '100000 '(json-encode [1 2 3 4 5]))                     ;; => Elapsed time: 4.123288s (1.940352s in 88 GCs)
(benchmark '100000 '(json-parse-string "[true]"))                  ;; => Elapsed time: 0.429458s (0.358050s in 18 GCs)
(benchmark '100000 '(json-read-from-string "[true]"))              ;; => Elapsed time: 3.136967s (1.578494s in 77 GCs)
(benchmark '100000 '(json-parse-string "{}"))                      ;; => Elapsed time: 0.943117s (0.821349s in 41 GCs)
(benchmark '100000 '(json-read-from-string "{}"))                  ;; => Elapsed time: 2.987505s (1.629616s in 73 GCs)
(benchmark '100000 '(json-parse-string json-string))               ;; => Elapsed time: 26.836383s (14.706035s in 728 GCs)
(benchmark '100000 '(json-read-from-string json-string))           ;; => Elapsed time: 29.445384s (17.515654s in 858 GCs)

@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