go 和 python 解析 markdown 速度比较

最近想把markdown 格式引入项目,首先来了解一下golang 的解析速度,以及与python 解析的对比。

go 和 python 解析 markdown 速度比较

python 冠军

python 解析 markdown 最快的是misaka

下面是在另外一台电脑上的比较结果:

1
2
3
4
5
6
7
Parsing the Markdown Syntax document 1000 times...
Mistune: 12.7255s
Mistune (with Cython): 9.74075s
Misaka: 0.550502s
Markdown: 46.4342s
Markdown2: 78.2267s
cMarkdown: 0.664128s

go 与 python

下面是同一台电脑,对python 和 go 解析markdown 速度的比较:

python

1
2
3
misaka 488.649129868 ms
markdown2 68.4613540173 s
markdown 40.3043971062 s

go

1
2
3
4
knieriem/markdown 35.854568 ms
blackfriday.MarkdownBasic 1.098207145 s
blackfriday.MarkdownCommon 1.607117325 s
opennota/markdown 3.277178642s

go 用的库

python 的测试代码

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
# coding: utf-8

import os
import time

NUM = 1000


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)
    t0 = time.time()
    for i in xrange(NUM):
        md.render(text)
    t1 = time.time()
    print 'misaka', (t1 - t0) * 1000, 'ms'


def benchmark_markdown2(text):
    import markdown2
    extras = ['code-friendly', 'fenced-code-blocks', 'footnotes']
    t0 = time.time()
    for i in xrange(NUM):
        markdown2.markdown(text, extras=extras)
    t1 = time.time()
    print 'markdown2', (t1 - t0), 's'


def benchmark_markdown(text):
    import markdown
    t0 = time.time()
    for i in xrange(NUM):
        markdown.markdown(text, ['extra'])
    t1 = time.time()
    print 'markdown', (t1 - t0), 's'


if __name__ == '__main__':
    root = os.path.dirname(__file__)
    filepath = os.path.join(
        root, 'markdown_documentation_syntax.text'
    )
    with open(filepath, 'r') as f:
        text = f.read()

    benchmark_misaka(text)
    benchmark_markdown2(text)
    benchmark_markdown(text)

go 的测试代码

Go:
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
package main

import (
	//"bufio"
	"bytes"
	"fmt"
	"github.com/knieriem/markdown"
	"github.com/russross/blackfriday"
	//"os"
	"io/ioutil"
	"strings"
	"time"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func main() {
	//p := NewParser(nil)
	p := markdown.NewParser(&markdown.Extensions{Smart: true})

	dat, err := ioutil.ReadFile("markdown_documentation_syntax.text")
	check(err)
	//fmt.Println(dat)
	//fmt.Println(string(dat))

	var buf bytes.Buffer

	r2 := strings.NewReader(string(dat))
	fhtml := markdown.ToHTML(&buf)
	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		p.Markdown(r2, fhtml)
	}
	t2 := time.Now()
	fmt.Println("knieriem/markdown", t2.Sub(t1))

	//output := blackfriday.MarkdownBasic(dat)
	//fmt.Println(output)
	//fmt.Println(string(output))

	t1 = time.Now()
	for j := 0; j < 1000; j++ {
		blackfriday.MarkdownBasic(dat)
	}
	t2 = time.Now()
	fmt.Println("blackfriday.MarkdownBasic", t2.Sub(t1))

	t1 = time.Now()
	for j := 0; j < 1000; j++ {
		blackfriday.MarkdownCommon(dat)
	}
	t2 = time.Now()
	fmt.Println("blackfriday.MarkdownCommon", t2.Sub(t1))

	fmt.Println("done")
}

例子2

github.com/opennota/markdown sample

Go:
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
package main

import (
	//"bufio"
	//"bytes"
	"fmt"
	"github.com/opennota/markdown"
	//"os"
	"io/ioutil"
	//"strings"
	"time"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func main() {

	dat, err := ioutil.ReadFile("markdown_documentation_syntax.text")
	check(err)

	md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true))

	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		//fmt.Println(md.RenderToString(dat))
		md.RenderToString(dat)
	}
	t2 := time.Now()
	fmt.Println("opennota/mdtool", t2.Sub(t1))

	fmt.Println("done")
}

本文网址: https://pylist.com/topic/100.html 转摘请注明来源

Suggested Topics

python 解析电子书的信息

epub 书是可供人们下载的开放性资源格式的电子图书。epub 文件通常与类似亚马逊Kindle 这样的电子阅读器不兼容。...

给ssdb python 接口提速

SSDB 是个新兴的数据库,其数据库的特点简单,性能高效,有好多python 接口,个人比较后选择一个最理想的,但还有提速空间,这里仅作经验分享。...

python 正确计算大文件md5 值

python 计算文件的md5值很方便,但如果只是简单的把文件都入到内存中,大文件会导致问题,一般采用切片的方式分段计算,下面的几个函数可以很好的解决这个问题。...

Leave a Comment