go json 解析速度测试

go json 解析速度测试

package main

import (
  "fmt"
 "encoding/json"
. "testing"
)

type OneField struct {
	A int
}

type ManyFields struct{
	A int
	B string
	C int32
	D *OneField

}


func main() {
	fmt.Println("Hello, playground")
	
	smallStruct := OneField{}
	fmt.Println(AllocsPerRun(10000, func(){
		json.Marshal(smallStruct)
	}))
	
	
	largeStruct := ManyFields{}
	fmt.Println(AllocsPerRun(10000, func(){
		json.Marshal(largeStruct)
	}))


	fmt.Println(AllocsPerRun(10000, func(){
		json.Marshal("hello")
	}))
	
	simpleMap := map[string]interface{}{"name": "name", "mail": "mail"}
	
	fmt.Println(AllocsPerRun(10000, func(){
		json.Marshal(simpleMap)
	}))
	
	
}
Relative Articles