From 0b0f32f5d30e6b3080dfc2e2af69c721111f43b2 Mon Sep 17 00:00:00 2001 From: Supreet Sethi Date: Tue, 3 Sep 2024 23:57:38 +1000 Subject: [PATCH] added a fastest generalizable implementaion of word_count --- wordcount bench/benchmark_test.go | 4 ++++ wordcount bench/main.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/wordcount bench/benchmark_test.go b/wordcount bench/benchmark_test.go index 06137ae..49e58c9 100644 --- a/wordcount bench/benchmark_test.go +++ b/wordcount bench/benchmark_test.go @@ -12,6 +12,10 @@ func BenchmarkCountWords(b *testing.B) { countWords(bytes) } +func BenchmarkFastestCountWords(b *testing.B) { + fastestCountWords("test_data.txt") +} + func BenchmarkByteCountWordsV2(b *testing.B) { f, err := os.Open("test_data.txt") if err != nil { diff --git a/wordcount bench/main.go b/wordcount bench/main.go index 5f0b025..7db10ac 100644 --- a/wordcount bench/main.go +++ b/wordcount bench/main.go @@ -19,6 +19,13 @@ func main() { var wordCount = countWords(bytes) fmt.Printf("countWords: %d words, duration: %dns\n", wordCount, time.Since(start)/1000) + start = time.Now() + wordCount, err := fastestCountWords(filePath) + if err != nil { + fmt.Printf("%d", err) + } + fmt.Printf("fastestCounWords: %d words, duration: %dns\n", wordCount, time.Since(start)/1000) + start = time.Now() f, err := os.Open(os.Args[1]) if err != nil { @@ -45,6 +52,29 @@ func ReadFile(filePath string) ([]byte, error) { return bytes, nil } +// Producing the most generalizable and fast word count +func fastestCountWords(filePath string) (int, error) { + fd, err := os.Open(filePath) + if err != nil { + return 0, fmt.Errorf("could not open file %q: %v", filePath, err) + } + defer fd.Close() + + words := 0 + scanner := bufio.NewScanner(fd) + scanner.Split(bufio.ScanWords) + + for scanner.Scan() { + words++ + } + + if err := scanner.Err(); err != nil { + return 0, fmt.Errorf("error reading file %q: %v", filePath, err) + } + + return words, nil +} + func countWords(bytes []byte) int { text := string(bytes) words := strings.Fields(text)