Skip to content

Instantly share code, notes, and snippets.

@tumdum
Last active January 24, 2016 20:24
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 tumdum/83581aee2693a60f5133 to your computer and use it in GitHub Desktop.
Save tumdum/83581aee2693a60f5133 to your computer and use it in GitHub Desktop.
Cost of string
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
file, err := os.Open(flag.Args()[0])
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var i int
// Compile regular expression first
r := regexp.MustCompile(`sshd\[\d{5}\]:\s*Failed`)
for scanner.Scan() {
line := scanner.Bytes()
if r.Match(line) {
i++
}
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
fmt.Println(i)
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
file, err := os.Open(flag.Args()[0])
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var i int
// Compile regular expression first
r := regexp.MustCompile(`sshd\[\d{5}\]:\s*Failed`)
for scanner.Scan() {
line := scanner.Text()
if r.MatchString(line) {
i++
}
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
fmt.Println(i)
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment