youbbs
youbbs
5077 3 0

go+json 还是没有python+json 结合得完美,解析速度比python 慢

go 有 map
python 有 dictionary

速度也没python 的快,测试数据:

go 1.4.2

loads  121.017531ms
go-ujson loads  63.217203ms
simplejson loads  90.202043ms
dumps  88.908435ms

python 2.7.3

simplejson
Loads Taken 14.655828476ms
Dumps Taken 28.5069942474ms

ujson
Loads Taken 12.7279758453
Dumps Taken 13.9889717102

json
Loads Taken 36.0250473022
Dumps Taken 23.5030651093
0

See Also

Nearby


Discussion (3)

youbbs
youbbs 2015-06-10 14:08

go 测试代码:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/bitly/go-simplejson"
	"github.com/mreiferson/go-ujson"
	"time"
)

var JsonBytes = []byte(`{"field_1": true, "field_2": true, "field_3": "XXXXYX", "field_4": true, "field_5": true, "field_6": true, "field_7": false, "field_8": true, "field_9": false, "field_10": false, "field_11": {"p": {}, "m": {}, "d": ["5", "3", "4", "8", "9", "7", "1", "2", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"]}, "field_12": false, "field_13": false, "field_14": "LLLLLL", "field_15": true, "field_16": "en-US", "field_17": "2013-11-26T05:49:06.236458", "field_18": "Shengzhou", "field_19": true, "field_20": false, "field_21": false, "field_22": "xxxxx...@hotmail.com", "field_23": "http://www.xxxxxxx.com/yyyyyyyyy", "field_24": true, "field_25": true, "field_26": "0001-01-01T00:00:00", "field_27": true, "field_28": true, "field_29": false, "field_30": true, "field_31": "", "field_32": true, "field_33": true, "field_34": "wrqer@#$!WEF#@#DSDFFksdniwo3hOwR8ruCeNF.3IkeOe80Yko8n7HvpsrZm", "field_35": false, "field_36": true, "field_37": "0001-01-01T00:00:00", "field_38": "String values encode as JSON strings. InvalidUTF8Error will be returned if an invalid UTF-8 sequence is encountered", "field_39": "female", "field_40": true, "field_41": true, "field_42": "insadesmaenideni", "field_43": "xyxyxyxyx_1233305355583", "field_44": "US", "field_45": {}, "field_46": false, "field_47": 1353270713, "field_48": "2013-11-12T15:38:33.052768", "field_49": "2012-02-29T22:46:18", "field_50": true}`)
var JsonObj map[string]interface{}

func init() {
	json.Unmarshal(JsonBytes, &JsonObj)
}

func BenchmarkJsonDecode() {
	var r map[string]interface{}
	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		//var r struct{}
		json.Unmarshal(JsonBytes, &r)
	}
	t2 := time.Now()
	fmt.Println("loads ", t2.Sub(t1))
}

func BenchmarkJsonDecode2() {
	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		_, err := ujson.NewFromBytes(JsonBytes)
		if err != nil {
			fmt.Println(err.Error())
		}
	}
	t2 := time.Now()
	fmt.Println("go-ujson loads ", t2.Sub(t1))
}

func BenchmarkJsonDecode3() {
	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		_, err := simplejson.NewJson(JsonBytes)
		if err != nil {
			fmt.Println(err.Error())
		}
	}
	t2 := time.Now()
	fmt.Println("simplejson loads ", t2.Sub(t1))
}

func BenchmarkJsonEncode() {
	t1 := time.Now()
	for j := 0; j < 1000; j++ {
		json.Marshal(JsonObj)
	}
	t2 := time.Now()
	fmt.Println("dumps ", t2.Sub(t1))
}

func main() {
	BenchmarkJsonDecode()
	BenchmarkJsonDecode2()
	BenchmarkJsonDecode3()
	BenchmarkJsonEncode()
}

go-ujson官方的loads (decode)的速度是标准库的两倍左右:

BenchmarkUjson    500000          4970 ns/op      20.12 MB/s
BenchmarkStdLib   200000         10323 ns/op       9.69 MB/s

https://github.com/mreiferson/go-ujson

0
youbbs
youbbs 2015-06-10 14:09

python 测试代码:

# coding=utf8

import time
# import simplejson as json
# import ujson as json
import json

NUM = 1000

# Loads

jsonStr = '{"field_1": true, "field_2": true, "field_3": "XXXXYX", "field_4": true, "field_5": true, "field_6": true, "field_7": false, "field_8": true, "field_9": false, "field_10": false, "field_11": {"p": {}, "m": {}, "d": ["5", "3", "4", "8", "9", "7", "1", "2", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"]}, "field_12": false, "field_13": false, "field_14": "LLLLLL", "field_15": true, "field_16": "en-US", "field_17": "2013-11-26T05:49:06.236458", "field_18": "Shengzhou", "field_19": true, "field_20": false, "field_21": false, "field_22": "xxxxx...@hotmail.com", "field_23": "http://www.xxxxxxx.com/yyyyyyyyy", "field_24": true, "field_25": true, "field_26": "0001-01-01T00:00:00", "field_27": true, "field_28": true, "field_29": false, "field_30": true, "field_31": "", "field_32": true, "field_33": true, "field_34": "wrqer@#$!WEF#@#DSDFFksdniwo3hOwR8ruCeNF.3IkeOe80Yko8n7HvpsrZm", "field_35": false, "field_36": true, "field_37": "0001-01-01T00:00:00", "field_38": "String values encode as JSON strings. InvalidUTF8Error will be returned if an invalid UTF-8 sequence is encountered", "field_39": "female", "field_40": true, "field_41": true, "field_42": "insadesmaenideni", "field_43": "xyxyxyxyx_1233305355583", "field_44": "US", "field_45": {}, "field_46": false, "field_47": 1353270713, "field_48": "2013-11-12T15:38:33.052768", "field_49": "2012-02-29T22:46:18", "field_50": true}'


def dec():
    for i in xrange(NUM):
        d = json.loads(jsonStr)

t0 = time.time()
dec()
t1 = time.time()
taken_ms = (t1 - t0) * 1000

print "Loads Taken", taken_ms, taken_ms / NUM

# Dumps

o = json.loads(jsonStr)


def enc():
    for i in xrange(NUM):
        json.dumps(o)

t0 = time.time()
enc()
t1 = time.time()
taken_ms = (t1 - t0) * 1000

print "Dumps Taken", taken_ms, taken_ms / NUM

python 代码好简洁!

0
李耀
李耀 2015-06-14 01:51

顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶

0
Login Topics