public
Created

syntax-parse is slow

  • Download Gist
gistfile1.txt
1 2 3
cpu time: 2269 real time: 2270 gc time: 1416
cpu time: 416 real time: 417 gc time: 0
cpu time: 184 real time: 184 gc time: 0
sp.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#lang racket
(require syntax/parse)
 
(define f1
(syntax-parser
#:literals (quote #%app)
[(quote #t) 1]
[(quote #f) 2]
[(#%app f) 3]
[(#%app f . x) 4]
[_ 5]))
 
(define f2
(λ (stx)
(syntax-case stx (quote #%app)
[(quote #t) 1]
[(quote #f) 2]
[(#%app f) 3]
[(#%app f . x) 4]
[_ 5])))
 
(define f3
(λ (stx)
(match (syntax->list stx)
[`(,(? identifier? (== #'quote free-identifier=?)) ,(app syntax-e #t)) 1]
[`(,(? identifier? (== #'quote free-identifier=?)) ,(app syntax-e #f)) 2]
[`(,(? identifier? (== #'#%app free-identifier=?)) ,f) 3]
[`(,(? identifier? (== #'#%app free-identifier=?)) ,f . ,x) 4]
[_ 5])))
 
(collect-garbage) (collect-garbage) (collect-garbage)
 
(time
(for ([i 100000])
(f1 #'(#%app + 1 2))))
(collect-garbage) (collect-garbage) (collect-garbage)
(time
(for ([i 100000])
(f2 #'(#%app + 1 2))))
(collect-garbage) (collect-garbage) (collect-garbage)
(time
(for ([i 100000])
(f3 #'(#%app + 1 2))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.