Skip to content

Instantly share code, notes, and snippets.

@samth
Created October 3, 2012 17:17
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 samth/3828408 to your computer and use it in GitHub Desktop.
Save samth/3828408 to your computer and use it in GitHub Desktop.
syntax-parse is slow
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
#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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment