我正在将文本模式扫描仪从Python3转换为Go1.10,但惊讶的是它实际上慢了2倍。经分析,罪魁祸首在strings.Contains()。请参阅下面的简单基准。我有什么想念的吗?您能推荐一种更快的Go模式搜索算法,这种算法在这种情况下会更好吗?我不必担心启动时间,相同的模式将用于扫描数百万个文件。
Py3基准:
import time
import re
RUNS = 10000
if __name__ == '__main__':
with open('data.php') as fh:
testString = fh.read()
def do():
return "576ad4f370014dfb1d0f17b0e6855f22" in testString
start = time.time()
for i in range(RUNS):
_ = do()
duration = time.time() - start
print("Python: %.2fs" % duration)
Go1.10基准测试:
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"time"
)
const (
runs = 10000
)
func main() {
fname := "data.php"
testdata := readFile(fname)
needle := "576ad4f370014dfb1d0f17b0e6855f22"
start := time.Now()
for i := 0; i < runs; i++ {
_ = strings.Contains(testdata, needle)
}
duration := time.Now().Sub(start)
fmt.Printf("Go: %.2fs\n", duration.Seconds())
}
func readFile(fname string) string {
data, err := ioutil.ReadFile(fname)
if err != nil {
log.Fatal(err)
}
return string(data)
}
data.php是528KB的文件,可以在这里找到。
输出:
Go: 1.98s
Python: 0.84s
相关分类