Golang: How do I determine the number of lines in a file efficiently?

SunSparc :

In Golang, I am looking for an efficient way to determine the number of lines a file has.

Of course, I can always loop through the entire file, but does not seem very efficient.

file, _ := os.Open("/path/to/filename")
fileScanner := bufio.NewScanner(file)
lineCount := 0
for fileScanner.Scan() {
    lineCount++
}
fmt.Println("number of lines:", lineCount)

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

JimB :

Here's a faster line counter using bytes.Count to find the newline characters.

It's faster because it takes away all the extra logic and buffering required to return whole lines, and takes advantage of some assembly optimized functions offered by the bytes package to search characters in a byte slice.

Larger buffers also help here, especially with larger files. On my system, with the file I used for testing, a 32k buffer was fastest.

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
        }
    }
}

and the 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I determine the number of changed files and changed lines per file type in a remote gitlab repository

How do I count number of lines in an imported to array CSV file?

How do I extract every consecutive pair of lines at a time from a file efficiently?

How do I efficiently determine the scale factor of two parallel vectors?

How do I navigate efficiently through emacs buffer modifying lines

How do I Do a Determine if a number in a array is a even number

How do I determine the MIME type of a file?

How do I determine file type in Linux?

How do I determine file encoding?

Determine the number of lines within a text file

Python: How do I split a .txt file into two or more files with the same number of lines in each?

How do I count the number of lines in a imported text file? C++

How do I write a program in C# that reads a text file and outputs the total number of lines?

How do I fetch lines in a log file

How do I comment lines in .plist file?

how do I open file to print lines?

How do i fix the "wrong number of fields" with the missing commas in CSV file in golang?

How to efficiently loop through the lines of a file in Bash?

How do I determine the number of values assumed by a "repeating" function in Haskell?

How do I determine the number of RAM slots in use?

How do I determine the row number of the row the function is executed in? (VBA)

How do I determine the number of arguments a function takes in Haxe?

How do I determine the number of significant figures in data in R?

How do I determine whether a randomly generated number is a multiple of 150?

How do I determine the number range by pressing the button in react native?

How do I determine the partition number for a extended partition in grub 2

How do I determine the number of rows and columns in a Swift [[AnyObject]]

How to efficiently store html response to a file in golang

How can I efficiently write the (700,000 lines) content from a For-loop into a file efficiently from Java?