Skip to content

Instantly share code, notes, and snippets.

@zhenjl
Last active December 27, 2015 23:29
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 zhenjl/7405913 to your computer and use it in GitHub Desktop.
Save zhenjl/7405913 to your computer and use it in GitHub Desktop.
package codesearch
import (
"bufio"
"compress/gzip"
"encoding/binary"
"fmt"
"github.com/reducedb/cityhash"
"log"
"os"
"runtime"
"runtime/pprof"
"strconv"
"testing"
"time"
)
func readFileOfIntegers(path string) ([]int32, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
gunzip, err := gzip.NewReader(file)
if err != nil {
log.Printf("encoding/readFileOfIntegers: Error opening gzip reader: %v\n", err)
return nil, err
}
result := make([]int32, 0, 50000000)
scanner := bufio.NewScanner(gunzip)
for scanner.Scan() {
i, e := strconv.ParseUint(scanner.Text(), 10, 32)
if e != nil {
log.Printf("readFileOfIntegers: Error reading from %s. %v\n", path, e)
} else {
result = append(result, int32(i))
}
}
// Run the garbage collector to get rid of all the strings that's been allocated
// during the file read
runtime.GC()
return result, scanner.Err()
}
func TestLatencyIntegers(t *testing.T) {
data, err := readFileOfIntegers("latency.txt.gz")
if err != nil {
log.Fatal(err)
}
b := make([]byte, len(data)*4)
for i := 0; i < len(data); i++ {
binary.LittleEndian.PutUint32(b[i*4:], uint32(data[i]))
}
f, e := os.Create("cpu.prof")
if e != nil {
log.Fatal(e)
}
defer f.Close()
now := time.Now()
pprof.StartCPUProfile(f)
cityhash.CityHash128(b, uint32(len(b)))
pprof.StopCPUProfile()
fmt.Printf("duration = %d\n", time.Since(now).Nanoseconds())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment