Created
July 17, 2013 14:29
-
-
Save ryochack/6021071 to your computer and use it in GitHub Desktop.
open/read vs open/mmap vs open/bufio.read on golang.
syscall.Madvise() is unsupported darwin.
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 ( | |
"fmt" | |
"log" | |
"os" | |
"syscall" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
return | |
} | |
path := os.Args[1] | |
f, err := os.Open(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fi, err := f.Stat() | |
if err != nil { | |
log.Fatal(err) | |
} | |
mem, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()), | |
syscall.PROT_READ, syscall.MAP_SHARED) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = syscall.Madvise(mem, syscall.MADV_SEQUENTIAL | syscall.MADV_WILLNEED) | |
if err != nil { | |
log.Fatal(err) | |
} | |
cnt := 0 | |
for _, b := range mem { | |
if b == 0x00 { | |
cnt++ | |
} | |
} | |
err = syscall.Munmap(mem) | |
if err != nil { | |
log.Fatal(err) | |
} | |
f.Close() | |
fmt.Printf("%s: size=%d cnt=%d\n", path, fi.Size(), cnt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment