Skip to content

Instantly share code, notes, and snippets.

@teraPacket
Created May 9, 2017 02:42
Show Gist options
  • Select an option

  • Save teraPacket/f02b6b3eaccedd1396add7f1a2b65ee1 to your computer and use it in GitHub Desktop.

Select an option

Save teraPacket/f02b6b3eaccedd1396add7f1a2b65ee1 to your computer and use it in GitHub Desktop.

Revisions

  1. teraPacket created this gist May 9, 2017.
    43 changes: 43 additions & 0 deletions json_mini_bench.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    //With the updated json parsing as of May 8, 2017, the speed of extraction from JSON string increased more than twice:
    //using old API it took 7.2 seconds, with new API, it took 2.3 sec)
    //the new API allows decoding JSON with a sequence of field names to specify the element to be extracted.
    package main

    import "time"
    import (
    "fmt"
    "github.com/xiaost/jsonport"
    )

    func main() {
    jsonstr := `{
    "foo": 1,
    "bar": 2,
    "test": "Hello, world!",
    "baz": 123.1,
    "array": [
    {"foo": 1},
    {"bar": 2},
    {"baz": 3}
    ],
    "subobj": {
    "foo": 1,
    "subarray": [1,2,3],
    "subsubobj": {
    "bar": 2,
    "baz": 3,
    "array": ["hello", "world"]
    }
    },
    "bool": true
    }`;

    sum := 1
    t0 := time.Now()
    for sum < 1000000 {
    jsonport.Unmarshal([]byte(jsonstr), "subobj","subsubobj", "array", 1)
    sum += 1
    }
    t1 := time.Now()
    fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
    }