-
Notifications
You must be signed in to change notification settings - Fork 262
Closed
Description
Just tried with jsparser, I realise it consume lowest memory possible, less than 9MB, probably avoid extra allocation iirc.
macOS Catalina
Go version 1.14rc1
Timing to complete: <2.2s
Memory: <9MB
package main
import (
"os"
"bufio"
"fmt"
"strconv"
"github.com/tamerh/jsparser"
)
func main() {
f, _ := os.Open("/tmp/1.json")
br := bufio.NewReaderSize(f, 16384)
parser := jsparser.NewJSONParser(br, "coordinates").SkipProps([]string{"name", "opts"})
x, y, z := 0.0, 0.0, 0.0; len := 0.0
for json := range parser.Stream() {
xx, _ := strconv.ParseFloat(json.ObjectVals["x"].StringVal, 64)
yy, _ := strconv.ParseFloat(json.ObjectVals["y"].StringVal, 64)
zz, _ := strconv.ParseFloat(json.ObjectVals["z"].StringVal, 64)
x += xx
y += yy
z += zz
len += 1.0
}
fmt.Printf("%.8f\n%.8f\n%.8f\n", x/len, y/len, z/len)
}
Metadata
Metadata
Assignees
Labels
No labels