Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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