go - golang: How do I determine the number of lines in a file efficiently? -


in golang, looking efficient way determine number of lines file has.

of course, can loop through entire file, not seem efficient.

file, _ := os.open("/path/to/filename") filescanner := bufio.newscanner(file) linecount := 0 filescanner.scan() {     linecount++ } fmt.println("number of lines:", linecount) 

is there better (quicker, less expensive) way find out how many lines file has?

here's faster line counter using bytes.count find newline characters.

it's faster because takes away logic , buffering required return whole lines, , takes advantage of assembly optimized functions offered bytes package search characters in byte slice.

larger buffers here, larger files. on system, file used testing, 32k buffer fastest.

func linecounter(r io.reader) (int, error) {     buf := make([]byte, 32*1024)     count := 0     linesep := []byte{'\n'}      {         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         }     } } 

and benchmark output:

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 

Comments