压测 正则 性能分析

 

main.go
package main

import (
"fmt"
"regexp"
"strings"
)

var path = "http://qq.pinyin.cn/?BizName=a123&BizStatus=b456&Test=qwer"

func Calculate1() (ret string) {
f := "[?&]{1}BizName=\\w{0,}[?&]{1}"
reg := regexp.MustCompile(f)
ret = reg.FindString(path)
ret = strings.Replace(ret, "&", "", -1)
ret = strings.Replace(ret, "BizName=", "", -1)
return
}

func Calculate2() (ret string) {
sli := strings.Split(path, "BizName=")
ret = sli[1]
ret = strings.Replace(ret, "&", "", -1)
return
}

func main() {

fmt.Println("main")
}

 

 

main_test.go

package main

import "testing"

func BenchmarkCalculate1(b *testing.B) {
for i := 0; i < b.N; i++ {
Calculate1()
}
}
func BenchmarkCalculate2(b *testing.B) {
for i := 0; i < b.N; i++ {
Calculate2()
}
}

 

test@a:~/gokit/tregexp$ go test -bench=.
goos: linux
goarch: amd64
pkg: t
cpu: AMD Ryzen 5 3600 6-Core Processor
BenchmarkCalculate1 283473 3962 ns/op
BenchmarkCalculate2 7610416 165.6 ns/op
PASS
ok t 2.591s
test@a:~/gokit/tregexp$

 

上一篇:Java8中使用lambda不为null时才过滤值


下一篇:Leetcode【43】Multiply Strings