如何测量前后字节大小和压缩时间

我想 gzip 一个字符串(它实际上是一个 JSON 响应)


var b bytes.Buffer

gz := gzip.NewWriter(&b)

if _, err := gz.Write([]byte("YourDataHere")); err != nil {

    panic(err)

}

如何轻松地输出压缩前后的字节大小,更重要的是,我如何确定压缩和解压缩回字符串所需的时间?


婷婷同学_
浏览 142回答 3
3回答

白衣染霜花

您可以根据 Nipun Talukdar 的评论计算大小。len([]byte("YourDataHere"))b.Len()并使用time.Now()和time.Since()来获取所花费的时间。var b bytes.Bufferinput := []byte("YourDataHere")fmt.Println("Input size : ", len(input))gz := gzip.NewWriter(&b)start := time.Now() gz.Write(input)if _, err := gz.Flush(); err != nil {        panic(err)}totalTime := time.Since(start) fmt.Println("Compressed size : ", b.Len(), "\nTime taken : ", totalTime)gz.Close()解压也可以应用相同的方法。您还可以创建一个可以进行计时的支持功能。func timer(startTime time.Time) {totalTime := time.Since(startTime)log.Println("Time taken : ",totalTime)}用法:defer timer(time.Now())在函数的开头。

皈依舞

这里有如何在 Go 中执行此操作的示例https://golang.org/test/bench/go1/gzip_test.go谢天谢地,它是 BSD 许可的......// Copyright 2011 The Go Authors.&nbsp; All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// This benchmark tests gzip and gunzip performance.package go1&nbsp;import (&nbsp; "bytes"&nbsp; gz "compress/gzip"&nbsp; "io"&nbsp; "io/ioutil"&nbsp; "testing")var (&nbsp; jsongunz = bytes.Repeat(jsonbytes, 10)&nbsp;&nbsp; jsongz&nbsp; &nbsp;[]byte)func init() {&nbsp; var buf bytes.Buffer&nbsp; c := gz.NewWriter(&buf)&nbsp; c.Write(jsongunz)&nbsp; c.Close()&nbsp; jsongz = buf.Bytes()}func gzip() {&nbsp; c := gz.NewWriter(ioutil.Discard)&nbsp; if _, err := c.Write(jsongunz); err != nil {&nbsp; &nbsp; panic(err)&nbsp; }&nbsp; if err := c.Close(); err != nil {&nbsp; &nbsp; panic(err)&nbsp; }}func gunzip() {&nbsp; r, err := gz.NewReader(bytes.NewBuffer(jsongz))&nbsp; if err != nil {&nbsp; &nbsp; panic(err)&nbsp; }&nbsp; if _, err := io.Copy(ioutil.Discard, r); err != nil {&nbsp; &nbsp; panic(err)&nbsp; }&nbsp; r.Close()}func BenchmarkGzip(b *testing.B) {&nbsp; b.SetBytes(int64(len(jsongunz)))&nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; gzip()&nbsp; }}func BenchmarkGunzip(b *testing.B) {&nbsp; b.SetBytes(int64(len(jsongunz)))&nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; gunzip()&nbsp; }}

一只名叫tom的猫

不幸的是,gzip.Writer无法报告写入底层流的压缩字节数。当底层流不在内存中时,它会变得更加复杂。为了解决这个问题,我写了一个“计数io.Writer”,我把它放在gzip.Writer和底层流之间,这样我就可以计算和提取写入的压缩字节数。在Go Playground 中尝试以下代码。package mainimport (&nbsp; &nbsp; "compress/gzip"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "os")// countingWriter is an io.Writer that counts the total bytes written to it.type countingWriter struct {&nbsp; &nbsp; w&nbsp; &nbsp; &nbsp;io.Writer&nbsp; &nbsp; Count int}var _ io.Writer = &countingWriter{}func newCountingWriter(w io.Writer) *countingWriter {&nbsp; &nbsp; return &countingWriter{w: w}}func (cw *countingWriter) Write(p []byte) (int, error) {&nbsp; &nbsp; n, err := cw.w.Write(p)&nbsp; &nbsp; cw.Count += n&nbsp; &nbsp; return n, err}func ExampleUse(w io.Writer) (int, error) {&nbsp; &nbsp; cw := newCountingWriter(w)&nbsp; &nbsp; zw, err := gzip.NewWriterLevel(cw, gzip.BestCompression)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; }&nbsp; &nbsp; if _, err := zw.Write([]byte("hello world")); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return cw.Count, err&nbsp; &nbsp; }&nbsp; &nbsp; err = zw.Close()&nbsp; &nbsp; return cw.Count, err}func main() {&nbsp; &nbsp; n, err := ExampleUse(os.Stderr)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("wrote %d bytes\n", n)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go