bytes.Count

它更快,因为它消除了返回整行所需的所有额外逻辑和缓冲,并利用字节包提供的一些汇编优化函数来搜索字节片中的字符 .

较大的缓冲区也有帮助,特别是对于较大的文件 . 在我的系统上,使用我用于测试的文件,32k缓冲区最快 .

func lineCounter(r io.Reader) (int, error) {
    buf := make([]byte, 32*1024)
    count := 0
    lineSep := []byte{'\n'}

    for {
        c, err := r.Read(buf)
        count += bytes.Count(buf[:c], lineSep)

        switch {
        case err == io.EOF:
            return count, nil

        case err != nil:
            return count, err
        }
    }
}

和基准输出:

BenchmarkBuffioScan   500      6408963 ns/op     4208 B/op    2 allocs/op
BenchmarkBytesCount   500      4323397 ns/op     8200 B/op    1 allocs/op
BenchmarkBytes32k     500      3650818 ns/op     65545 B/op   1 allocs/op