func md5sum1(file string) string {
data, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
return fmt.Sprintf("%x", md5.Sum(data))
}
var test_path = "/path/to/file"
func BenchmarkMd5Sum1(b *testing.B) {
for i := 0; i < b.N; i++ {
md5sum1(test_path)
}
}
go test -test.run=none -test.bench="^BenchmarkMd5Sum1$" -benchtime=10s -benchmem BenchmarkMd5Sum1-4 300 43704982 ns/op 19408224 B/op 14 allocs/op PASS ok tmp 17.446s
// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
func ReadFile(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
// It's a good but not certain bet that FileInfo will tell us exactly how much to
// read, so let's try it but be prepared for the answer to be wrong.
var n int64
if fi, err := f.Stat(); err == nil {
// Don't preallocate a huge buffer, just in case.
if size := fi.Size(); size < 1e9 {
n = size
}
}
// As initial capacity for readAll, use n + a little extra in case Size is zero,
// and to avoid another allocation after Read has filled the buffer. The readAll
// call will read into its allocated internal buffer cheaply. If the size was
// wrong, we'll either waste some space off the end or reallocate as needed, but
// in the overwhelmingly common case we'll get it just right.
// readAll 第二个参数是即将创建的 buffer 大小
return readAll(f, n+bytes.MinRead)
}
func readAll(r io.Reader, capacity int64) (b []byte, err error) {
// 这个 buffer 的大小就是 file size + bytes.MinRead
buf := bytes.NewBuffer(make([]byte, 0, capacity))
// If the buffer overflows, we will get bytes.ErrTooLarge.
// Return that as an error. Any other panic remains.
defer func() {
e := recover()
if e == nil {
return
}
if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
err = panicErr
} else {
panic(e)
}
}()
_, err = buf.ReadFrom(r)
return buf.Bytes(), err
}
func md5sum2(file string) string {
f, err := os.Open(file)
if err != nil {
return ""
}
defer f.Close()
h := md5.New()
_, err = io.Copy(h, f)
if err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func BenchmarkMd5Sum2(b *testing.B) {
for i := 0; i < b.N; i++ {
md5sum2(test_path)
}
}
$ go test -test.run=none -test.bench="^BenchmarkMd5Sum2$" -benchtime=10s -benchmem BenchmarkMd5Sum2-4 500 37538305 ns/op 33093 B/op 8 allocs/op PASS ok tmp 22.657s
func md5sum3(file string) string {
f, err := os.Open(file)
if err != nil {
return ""
}
defer f.Close()
r := bufio.NewReader(f)
h := md5.New()
_, err = io.Copy(h, r)
if err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func BenchmarkMd5Sum3(b *testing.B) {
for i := 0; i < b.N; i++ {
md5sum3(test_path)
}
}
$ go test -test.run=none -test.bench="^BenchmarkMd5Sum3$" -benchtime=10s -benchmem BenchmarkMd5Sum3-4 300 42589812 ns/op 4507 B/op 9 allocs/op PASS ok tmp 16.817s
func Copy(dst Writer, src Reader) (written int64, err error) {
return copyBuffer(dst, src, nil)
}
// copyBuffer is the actual implementation of Copy and CopyBuffer.
// if buf is nil, one is allocated.
func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
// hash.Hash 这个 Writer 并没有实现 WriteTo 方法,所以不会走这里
if wt, ok := src.(WriterTo); ok {
return wt.WriteTo(dst)
}
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
// 而 bufio.Reader 实现了 ReadFrom 方法,所以,会走这里
if rt, ok := dst.(ReaderFrom); ok {
return rt.ReadFrom(src)
}
if buf == nil {
buf = make([]byte, 32*1024)
}
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = ErrShortWrite
break
}
}
if er == EOF {
break
}
if er != nil {
err = er
break
}
}
return written, err
}
// Md5Sum2 用 CopyBufer 重新实现,buf := make([]byte, 4096) BenchmarkMd5Sum2-4 500 38484425 ns/op 4409 B/op 8 allocs/op BenchmarkMd5Sum3-4 500 38671090 ns/op 4505 B/op 9 allocs/op
$ go test -test.run=none -test.bench="." -benchtime=10s -benchmem testing: warning: no tests to run BenchmarkMd5Sum1-4 300 42551920 ns/op 19408230 B/op 14 allocs/op BenchmarkMd5Sum2-4 500 38445352 ns/op 33089 B/op 8 allocs/op BenchmarkMd5Sum3-4 500 38809429 ns/op 4505 B/op 9 allocs/op PASS ok tmp 63.821s
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有