Skip to content

Instantly share code, notes, and snippets.

@apg

apg/Makefile Secret

Last active Oct 18, 2017
Embed
What would you like to do?
import json
import random
import string
def rstr(N):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
data = {}
for i in range(100000):
x = {}
for j in range(10):
x[rstr(10)] = rstr(100)
data[str(i)] = x
with open('huge.json', 'w') as y:
json.dump(data, y)
package main
import (
"encoding/json"
"fmt"
"os"
"time"
)
func main() {
input, err := os.Open("huge.json")
if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}
var i interface{}
before := time.Now()
dec := json.NewDecoder(input)
err = dec.Decode(&i)
if err != nil {
fmt.Printf("Encode error: %v\n", err)
os.Exit(1)
}
fmt.Println("LOAD: ", time.Since(before))
output, err := os.Create("out.go.json")
if err != nil {
fmt.Printf("File error (write): %v\n", err)
os.Exit(1)
}
before = time.Now()
enc := json.NewEncoder(output)
err = enc.Encode(i)
if err != nil {
fmt.Printf("Encode error: %v\n", err)
os.Exit(1)
}
fmt.Println("DUMP: ", time.Since(before))
}
bench$ for n in `seq 1 3`; do make go python > /dev/null; done
bench$ make go python
go build ./go.go
echo "Running go version."
Running go version.
./go
LOAD: 2.016554434s
DUMP: 1.478200338s
echo "Running python version"
Running python version
python py.py
LOAD elapsed=1.56545710564
DUMP elapsed=3.07746505737
bench$ go version
go version go1.9 darwin/amd64
agwozd-ltm2:bench agwozdziewycz$ python --version
Python 2.7.10
bench$ du -h *
114M huge.json
112M out.go.json
114M out.py.json
go:
go build ./go.go
echo "Running go version."
./go
python:
echo "Running python version"
python py.py
.PHONY: go python
import json
import time
before = time.time()
with open('huge.json') as x:
obj = json.load(x)
print("LOAD elapsed={}".format(time.time() - before))
before = time.time()
with open('out.py.json', 'w') as y:
json.dump(obj, y)
print("DUMP elapsed={}".format(time.time() - before))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment