Created
May 9, 2017 02:42
-
-
Save teraPacket/f02b6b3eaccedd1396add7f1a2b65ee1 to your computer and use it in GitHub Desktop.
mini benchmark on extracting field from JSON string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment