-
Notifications
You must be signed in to change notification settings - Fork 38
/
benchmark.jl
51 lines (42 loc) · 1017 Bytes
/
benchmark.jl
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
44
45
46
47
48
using BenchmarkTools
using MacroTools
using MLStyle
using MLStyle.Pervasive: Push, PushTo, Many
ex = quote
struct Foo
x :: Int
y
end
end
function b_macrotools(ex)
@capture(ex, struct T_ fields__ end)
(T, fields)
end
function b_mlstyle(ex)
MLStyle.@match ex begin
Do(fields = []) &&
quote
$(::LineNumberNode)
struct $typename
$(
Many(
::LineNumberNode ||
:($name :: $typ) &&
Push(fields, (name, typ)) ||
(a :: Symbol) &&
Push(fields, (a, Any))
)...
)
end
end => (typename, fields)
end
end
@info b_macrotools(ex)
@info b_mlstyle(ex)
@btime b_macrotools(ex)
@btime b_mlstyle(ex)
# Output
# [ Info: (:Foo, Any[:(x::Int), :y])
# [ Info: (:Foo, Any[(:x, :Int), (:y, Any)])
# 18.824 μs (114 allocations: 6.41 KiB)
# 5.221 μs (32 allocations: 1.39 KiB)