go - How to measure both before and after byte size and time to compress -
i want gzip string (it json response)
var b bytes.buffer gz := gzip.newwriter(&b) if _, err := gz.write([]byte("yourdatahere")); err != nil { panic(err) }
how can output size of bytes before , after compression , more importantly how can time takes compress , decompress string?
you can calculate size per nipun talukdar's comment.
len([]byte("yourdatahere")) b.len()
and use time.now()
, time.since()
time taken.
var b bytes.buffer input := []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()
same method can applied unzipping. can create support function can timing.
func timer(starttime time.time) { totaltime := time.since(starttime) log.println("time taken : ",totaltime) }
usage : defer timer(time.now())
@ start of function.
Comments
Post a Comment