Created
November 16, 2009 04:17
-
-
Save jeffhill/235718 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( "bufio"; "fmt"; "io"; "log"; "os"; "regexp"; ) | |
| /* | |
| real 0m0.026s | |
| user 0m0.019s | |
| sys 0m0.006s | |
| */ | |
| func test1() { | |
| fileContent, err := io.ReadFile( "data.txt" ); | |
| if err != nil { | |
| log.Stderr( err ); | |
| return; | |
| } | |
| fmt.Printf("Read %v characters in test 1.\n", len(fileContent) ); | |
| } | |
| /* | |
| real 0m0.029s | |
| user 0m0.022s | |
| sys 0m0.006s | |
| */ | |
| func test2() { | |
| fileObj, err := os.Open( "data.txt", os.O_RDONLY, 0666 ); | |
| if err != nil { | |
| log.Stderr( err ); | |
| return; | |
| } | |
| r := bufio.NewReader( fileObj ); | |
| charCount := 0; | |
| for { | |
| line, err := r.ReadString('\n'); | |
| if err == os.EOF { | |
| fmt.Printf("Read %v characters in test 2.\n", charCount ); | |
| return; | |
| } | |
| if err != nil { | |
| log.Stderr( err ); | |
| return; | |
| } | |
| charCount += len(line); | |
| } | |
| } | |
| /* | |
| real 0m0.176s | |
| user 0m0.168s | |
| sys 0m0.007s | |
| */ | |
| func test3() { | |
| fileObj, err := os.Open( "data.txt", os.O_RDONLY, 0666 ); | |
| if err != nil { | |
| log.Stderr( err ); | |
| return; | |
| } | |
| r := bufio.NewReader( fileObj ); | |
| matcher := regexp.MustCompile( "H[a|b][m|n]let" ); | |
| charCount := 0; | |
| lineCount := 0; | |
| for { | |
| line, err := r.ReadString('\n'); | |
| lineCount++; | |
| if err == os.EOF { | |
| fmt.Printf("Read %v characters in test 3.\n", charCount ); | |
| return; | |
| } | |
| if err != nil { | |
| log.Stderr( err ); | |
| return; | |
| } | |
| if matcher.MatchString(line) { | |
| fmt.Printf("Matched at %v.\n", lineCount ); | |
| } | |
| charCount += len(line); | |
| } | |
| } | |
| func main() { | |
| //test1(); | |
| //test2(); | |
| test3(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment