咖啡伴侶

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

          go原生數(shù)據(jù)效率

          Posted on 2013-09-29 09:57 oathleo 閱讀(1167) 評論(0)  編輯  收藏 所屬分類: Golang
          接著上回,對象序列化和反序的效率已經(jīng)很高,試試原生數(shù)據(jù)的效率

          先上代碼
          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}
           
          結(jié)論:
          1.不管什么語言,大批量同類型數(shù)據(jù)的傳輸,原生性能還是比第三方序列化 效率高很多
          2.C++ 使用memcpy put 原始類型,效率還是比go高很多
          C++原生代碼性能:
          total record 1000000
          time pack 11 ms
          time unpack 57 ms
           
           
          主站蜘蛛池模板: 阳城县| 金湖县| 封丘县| 涡阳县| 清水河县| 波密县| 都兰县| 三明市| 巢湖市| 南和县| 明星| 大新县| 郓城县| 兰西县| 习水县| 宝丰县| 阿勒泰市| 湘潭县| 闻喜县| 昌邑市| 乐昌市| 秦皇岛市| 邻水| 二手房| 闻喜县| 蕉岭县| 永登县| 牙克石市| 勐海县| 北流市| 龙里县| 甘洛县| 白玉县| 新龙县| 耒阳市| 垦利县| 依兰县| 改则县| 汶上县| 徐汇区| 渭南市|