Posted on 2013-09-29 09:57
oathleo 閱讀(1166)
評論(0) 編輯 收藏 所屬分類:
Golang
接著上回,對象序列化和反序的效率已經很高,試試原生數據的效率
先上代碼
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])
}
123
total record: 1000000
send data: {999999 16959 0.69153386}
send cost: 93
rev cost: 61
rev data: {999999 16959 0.69153386}
結論:
1.不管什么語言,大批量同類型數據的傳輸,原生性能還是比第三方序列化 效率高很多
2.C++ 使用
memcpy put 原始類型,效率還是比go高很多
C++原生代碼性能:
total record 1000000
time pack 11 ms
time unpack 57 ms