在Go语言中,我们尽力提高效率。不要做不必要的事情。
例如,
package main
import (
"bufio"
"bytes"
"fmt"
"os"
)
func main() {
lines, requestA := 0, 0
f, err := os.Open("request.log")
if err != nil {
fmt.Print("There has been an error!: ", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines++
line := scanner.Bytes()
if len(line) <= 30 || line[30] != 'A' {
continue
}
if !bytes.Equal(line[22:], []byte("REQUEST-A")) {
continue
}
requestA++
request := string(line)
fmt.Println(request)
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
fmt.Println(lines, requestA)
}
输出:
$ go run request.go
2019-09-30T04:17:02 - REQUEST-A
2019-09-30T04:20:02 - REQUEST-A
2019-09-30T04:21:02 - REQUEST-A
6 3
$ cat request.log
2019-09-30T04:17:02 - REQUEST-A
2019-09-30T04:18:02 - REQUEST-C
2019-09-30T04:19:02 - REQUEST-B
2019-09-30T04:20:02 - REQUEST-A
2019-09-30T04:21:02 - REQUEST-A
2019-09-30T04:22:02 - REQUEST-B
为了强调效率的重要性(记录可能非常大),让我们对Markus W Mahlberg的解决方案运行基准测试:https://play.golang.org/p/R2D_BeiJvx9。
$ go test log_test.go -bench=. -benchmem
BenchmarkPeterSO-4 21285 56953 ns/op 4128 B/op 2 allocs/op
BenchmarkMarkusM-4 649 1817868 ns/op 84747 B/op 2390 allocs/op
log_test.go
:
package main
import (
"bufio"
"bytes"
"regexp"
"strings"
"testing"
)
var requestLog = `
2019-09-30T04:17:02 - REQUEST-A
2019-09-30T04:18:02 - REQUEST-C
2019-09-30T04:19:02 - REQUEST-B
2019-09-30T04:20:02 - REQUEST-A
2019-09-30T04:21:02 - REQUEST-A
2019-09-30T04:22:02 - REQUEST-B
`
var benchLog = strings.Repeat(requestLog[1:], 256)
func BenchmarkPeterSO(b *testing.B) {
for N := 0; N < b.N; N++ {
scanner := bufio.NewScanner(strings.NewReader(benchLog))
for scanner.Scan() {
line := scanner.Bytes()
if len(line) <= 30 || line[30] != 'A' {
continue
}
if !bytes.Equal(line[22:], []byte("REQUEST-A")) {
continue
}
request := string(line)
_ = request
}
if err := scanner.Err(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMarkusM(b *testing.B) {
for N := 0; N < b.N; N++ {
var re *regexp.Regexp = regexp.MustCompile(`^(\S*) - REQUEST-A$`)
scanner := bufio.NewScanner(strings.NewReader(benchLog))
var res []string
for scanner.Scan() {
if res = re.FindStringSubmatch(scanner.Text()); len(res) > 0 {
_ = res[1]
}
}
if err := scanner.Err(); err != nil {
b.Fatal(err)
}
}
}