http://my.oschina.net/u/1431106/blog/188628性能
接着上一章 spa
對象序列化和反序的效率已經很高,試試原生數據的效率.net
package main import ( "fmt" "math/rand" "opbuf" "time" ) type RTValue struct { Time int32 Status int16 Value float32 } func main() { size := 1000000 col := make([]RTValue, size) for i := 0; i < size; i++ { col[i] = RTValue{Time: int32(i), Status: int16(i), Value: rand.Float32()} } fmt.Println("send data:", col[size-1]) var opbuff *opbuf.OPBuffer = opbuf.NewOPBuffer() start := time.Now().UnixNano() for i := 0; i < size; i++ { // opbuff.PutByte(col[i].Data) opbuff.PutInt32(col[i].Time) opbuff.PutInt16(col[i].Status) opbuff.PutFloat32(col[i].Value) } fmt.Println("send cost:", (time.Now().UnixNano()-start)/1000000) opbuff.Flush() start = time.Now().UnixNano() for i := 0; i < size; i++ { col[i].Time,_ = opbuff.GetInt32() col[i].Status,_ = opbuff.GetInt16() col[i].Value,_ = opbuff.GetFloat32() } fmt.Println("rev cost:", (time.Now().UnixNano()-start)/1000000) fmt.Println("rev data:", col[size-1]) }
Go原生代碼性能:code
total record: 1000000對象
send data: {999999 16959 0.69153386}blog
send cost: 93get
rev cost: 61class
rev data: {999999 16959 0.69153386}效率
結論:import
1.無論什麼語言,大批量同類型數據的傳輸,原生性能仍是比第三方序列化 效率高不少
2.C++ 使用memcpy put 原始類型,效率仍是比go高不少
C++原生代碼性能:
total record 1000000
time pack 11 ms
time unpack 57 ms