Skip to content

Instantly share code, notes, and snippets.

@ssikdar1
Last active December 31, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssikdar1/f0902edc535e8dd0d70f7c0d23805dc4 to your computer and use it in GitHub Desktop.
Save ssikdar1/f0902edc535e8dd0d70f7c0d23805dc4 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"
)
func pattern_match(re *regexp.Regexp, buffer []byte) {
_ = re.Find(buffer)
}
func pattern_match2(re *regexp.Regexp, buffer []byte) {
_ = re.FindAll(buffer, -1)
}
func pattern_match_line_by_line(re *regexp.Regexp, scanner *bufio.Scanner) {
for scanner.Scan() {
_ = re.Find(scanner.Bytes())
}
}
func pattern_match_line_by_line2(re *regexp.Regexp, scanner *bufio.Scanner) {
for scanner.Scan() {
_ = re.FindAll(scanner.Bytes(), -1)
}
}
//Entire File?
func BenchmarkPattern1(b *testing.B) {
pattern1 := "([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?"
rep1 := regexp.MustCompile(pattern1)
file_path := "/Users/ssikdar/Downloads/howto2"
content_buff, _ := ioutil.ReadFile(file_path)
for n := 0; n < b.N; n++ {
pattern_match(rep1, content_buff)
}
}
func BenchmarkPattern1b(b *testing.B) {
b.ResetTimer()
pattern1 := "([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?"
rep1 := regexp.MustCompile(pattern1)
file_path := "/Users/ssikdar/Downloads/howto2"
content_buff, _ := ioutil.ReadFile(file_path)
for n := 0; n < b.N; n++ {
pattern_match2(rep1, content_buff)
}
}
//Line By Line?
func BenchmarkPattern1LineByLine(b *testing.B) {
pattern1 := "([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?"
rep1 := regexp.MustCompile(pattern1)
file_path := "/Users/ssikdar/Downloads/howto2"
for n := 0; n < b.N; n++ {
in_file, _ := os.Open(file_path)
defer in_file.Close()
scanner := bufio.NewScanner(in_file)
scanner.Split(bufio.ScanLines)
pattern_match_line_by_line(rep1, scanner)
}
}
//Line By Line?
func BenchmarkPattern1LineByLine2(b *testing.B) {
pattern1 := "([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?"
rep1 := regexp.MustCompile(pattern1)
file_path := "/Users/ssikdar/Downloads/howto2"
for n := 0; n < b.N; n++ {
in_file, _ := os.Open(file_path)
defer in_file.Close()
scanner := bufio.NewScanner(in_file)
scanner.Split(bufio.ScanLines)
pattern_match_line_by_line2(rep1, scanner)
}
}
func main() {
result := testing.Benchmark(BenchmarkPattern1)
fmt.Printf("BenchmarkPattern1\t%v\n", result)
result = testing.Benchmark(BenchmarkPattern1b)
fmt.Printf("BenchmarkPattern1b\t%v\n", result)
result = testing.Benchmark(BenchmarkPattern1LineByLine)
fmt.Printf("BenchmarkPattern1LineByLine\t%v\n", result)
result = testing.Benchmark(BenchmarkPattern1LineByLine2)
fmt.Printf("BenchmarkPattern1LineByLine2\t%v\n", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment