咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

go原生数据效率

Posted on 2013-09-29 09:57 oathleo 阅读(1153) 评论(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

Go原生代码性能:
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
 
 

只有注册用户登录后才能发表评论。


网站导航: