Skip to content

Instantly share code, notes, and snippets.

@yuguorui
Created April 9, 2022 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuguorui/9700aa236dcb6e7072de282310d3b866 to your computer and use it in GitHub Desktop.
Save yuguorui/9700aa236dcb6e7072de282310d3b866 to your computer and use it in GitHub Desktop.
package main
import (
"compress/zlib"
"fmt"
"math/rand"
"os"
"time"
"unsafe"
)
const LENGTH = 1e9
func generateRandomUint32() []uint32 {
buf := make([]uint32, LENGTH)
for i := 0; i < LENGTH; i++ {
buf = append(buf, rand.Uint32())
}
return buf
}
func WriteToFileRaw(data []byte) {
f, err := os.Create("data-raw")
if err != nil {
panic(err)
}
start := time.Now()
_, err = f.Write(data)
if err != nil {
panic(err)
}
f.Close()
elapsed := time.Since(start)
fmt.Println("WriteToFileRaw", elapsed)
}
func WriteToFileZlib(data []byte) {
start := time.Now()
f, err := os.Create("data-zlib")
if err != nil {
panic(err)
}
w, err := zlib.NewWriterLevel(f, zlib.BestSpeed)
if err != nil {
panic(err)
}
_, err = w.Write(data)
if err != nil {
panic(err)
}
w.Close()
f.Close()
elapsed := time.Since(start)
fmt.Println("WriteToFileZlib", elapsed)
}
func main() {
data := generateRandomUint32()
bdata := (*[LENGTH * 4]byte)(unsafe.Pointer(&data[0]))[:]
for i := 0; i < 5; i++ {
WriteToFileRaw(bdata)
WriteToFileZlib(bdata)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment