Count Lines faster than wc

master
DustyP 5 years ago
parent 681dc0445d
commit 05a7cdd9d7

@ -33,6 +33,8 @@ package main
// output version information and exit // output version information and exit
import ( import (
"fmt"
"github.com/droundy/goopt" "github.com/droundy/goopt"
"github.com/dustinpianalto/wc-go/pkg/wc" "github.com/dustinpianalto/wc-go/pkg/wc"
) )
@ -67,5 +69,11 @@ var (
func main() { func main() {
goopt.Version = "v0.0.0a" goopt.Version = "v0.0.0a"
goopt.Parse(nil) goopt.Parse(nil)
wc.Count("main.go", *fWords, *fChars, *fLines, *fBytes, *fMaxLineLength) if len(goopt.Args) == 0 {
fmt.Println(goopt.Help())
} else {
for _, a := range goopt.Args {
wc.Count(a, *fWords, *fChars, *fLines, *fBytes, *fMaxLineLength)
}
}
} }

@ -0,0 +1,19 @@
package wc
func GetLineCount(chunk []byte) int64 {
var count int64
for _, b := range chunk {
if b == '\n' {
count++
}
}
return count
}
func ConcurrentChunkCounter(chunks <-chan []byte, counts chan<- int64) {
var totalCount int64
for chunk := range chunks {
totalCount += GetLineCount(chunk)
}
counts <- totalCount
}

@ -1,14 +1,15 @@
package wc package wc
import ( import (
"bufio"
"fmt" "fmt"
"log" "io"
"os" "os"
"runtime"
) )
const BufferSize = 1024 * 1024
type Counter struct { type Counter struct {
FileReader *bufio.Reader
Words int64 Words int64
Chars int64 Chars int64
Lines int64 Lines int64
@ -27,28 +28,82 @@ func Count(filename string, cw, cc, cl, cb, mll bool) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return
} }
defer file.Close() defer file.Close()
fi, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
size := fi.Size()
processLine := cw || cc processLine := cw || cc
var c = Counter{FileReader: bufio.NewReader(file)}
if cl && !processLine { var c = &Counter{}
c.CountLines(cb) numWorkers := runtime.NumCPU()
chunks := make(chan []byte, numWorkers)
counts := make(chan int64, numWorkers)
for i := 0; i < numWorkers; i++ {
go ConcurrentChunkCounter(chunks, counts)
} }
fmt.Printf("%d %s\n", c.Lines, filename)
}
func (c *Counter) CountLines(cb bool) { if cl && !processLine {
for { for {
r, s, err := c.FileReader.ReadRune() buf := make([]byte, BufferSize)
log.Printf("%#v, %#v, %#v", r, s, err) bytes, err := file.Read(buf)
if err != nil { chunks <- buf[:bytes]
break if err != nil {
} if err == io.EOF {
if r == '\n' { break
c.Lines++ } else {
panic(err)
}
}
} }
if cb { close(chunks)
c.Bytes += int64(s) for i := 0; i < numWorkers; i++ {
c.Lines += <-counts
} }
close(counts)
}
if cb {
c.Bytes = size
}
if c.Lines > 0 {
fmt.Printf("%d ", c.Lines)
} }
if c.Words > 0 {
fmt.Printf("%d ", c.Words)
}
if c.Chars > 0 {
fmt.Printf("%d ", c.Words)
}
if c.Bytes > 0 {
fmt.Printf("%d ", c.Bytes)
}
if c.MaxLineLength > 0 {
fmt.Printf("%d ", c.MaxLineLength)
}
fmt.Printf("%s\n", filename)
} }
//func (c *Counter) CountLines() {
// for {
// _, p, err := c.FileReader.ReadLine()
// if err != nil && err != io.EOF {
// panic(err)
// }
// if err == io.EOF {
// break
// }
// if !p {
// c.Lines++
// }
// }
// c.Lines--
//}

Loading…
Cancel
Save