/
bench.py
92 lines (72 loc) · 2.19 KB
/
bench.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import time
class benchmark(object):
def __init__(self, name):
self._name = name
def __call__(self, func):
def wrapper(*args, **kwargs):
start = time.clock()
func(*args, **kwargs)
end = time.clock()
return end - start
wrapper.__name__ = func.__name__
return wrapper
@benchmark('mistune')
def benchmark_mistune(text):
import mistune
mistune.markdown(text)
@benchmark('misaka')
def benchmark_misaka(text):
import misaka as m
# mistune has all these features
extensions = (
m.EXT_NO_INTRA_EMPHASIS | m.EXT_FENCED_CODE | m.EXT_AUTOLINK |
m.EXT_TABLES | m.EXT_STRIKETHROUGH
)
md = m.Markdown(m.HtmlRenderer(), extensions=extensions)
md.render(text)
@benchmark('markdown2')
def benchmark_markdown2(text):
import markdown2
extras = ['code-friendly', 'fenced-code-blocks', 'footnotes']
markdown2.markdown(text, extras=extras)
@benchmark('markdown')
def benchmark_markdown(text):
import markdown
markdown.markdown(text, ['extra'])
@benchmark('cMarkdown')
def benchmark_cMarkdown(text):
import cMarkdown
cMarkdown.markdown(text)
@benchmark('discount')
def benchmark_discount(text):
import discount
discount.Markdown(text).get_html_content()
if __name__ == '__main__':
root = os.path.dirname(__file__)
filepath = os.path.join(
root, 'cases', 'markdown_documentation_syntax.text'
)
with open(filepath, 'r') as f:
text = f.read()
loops = 1000
totals = []
methods = [
('Mistune', benchmark_mistune),
('Misaka', benchmark_misaka),
('Markdown', benchmark_markdown),
('Markdown2', benchmark_markdown2),
('cMarkdown', benchmark_cMarkdown),
('Discount', benchmark_discount),
]
print('Parsing the Markdown Syntax document %d times...' % loops)
for i, method in enumerate(methods):
name = method[0]
fn = method[1]
try:
total = 0
for nth in range(0, loops):
total += fn(text)
print('%s: %gs' % (name, total))
except ImportError:
print('%s is not available' % name)