| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment