Last active
          September 27, 2017 16:24 
        
      - 
      
 - 
        
Save arnehormann/7368414 to your computer and use it in GitHub Desktop.  
    A pretty small Go application compressing from stdin to stdout as gzip.
  
        
  
    
      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 ( | |
| "compress/gzip" | |
| "fmt" | |
| "io" | |
| "os" | |
| "strconv" | |
| "time" | |
| ) | |
| func main() { | |
| level := gzip.DefaultCompression | |
| if len(os.Args) >= 2 { | |
| l, err := strconv.ParseInt(os.Args[1], 10, 0) | |
| if err != nil { | |
| panic(err) | |
| } | |
| level = int(l) | |
| } | |
| out, err := gzip.NewWriterLevel(os.Stdout, level) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer out.Close() | |
| before := time.Now().UnixNano() | |
| written, err := io.Copy(out, os.Stdin) | |
| duration := float64(time.Now().UnixNano() - before) | |
| if err != nil && err != io.EOF { | |
| panic(err) | |
| } | |
| fmt.Fprintf(os.Stderr, "Compressed %d bytes in %f ms\n", written, duration/(1000*1000)) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
I built this to compare compression speeds of Go, gzip and pigz.
It accepts one (optional) parameter: the compression level.
-1019See http://golang.org/pkg/compress/flate/#pkg-constants
Compressing the binary created with
go build gzipstream.gowithtime COMMAND < gzipstream > gzipstream.gztakes Go twice as long as gzip with the best compression (9). On my i7, pigz wins hands down (about 1/4 real time compared to gzip).