咖啡伴侶

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

          go原生數據效率

          Posted on 2013-09-29 09:57 oathleo 閱讀(1167) 評論(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
           
           
          主站蜘蛛池模板: 乌恰县| 元朗区| 米脂县| 扎兰屯市| 安塞县| 阜康市| 茌平县| 封丘县| 扎兰屯市| 宝坻区| 司法| 明溪县| 锡林浩特市| 云阳县| 南丰县| 迁安市| 南岸区| 土默特左旗| 特克斯县| 阿拉善左旗| 靖宇县| 望谟县| 武强县| 土默特左旗| 隆尧县| 牡丹江市| 和政县| 和平区| 鸡东县| 梁山县| 蒙城县| 宁远县| 津市市| 色达县| 平塘县| 清丰县| 信阳市| 西昌市| 上蔡县| 英超| 潞西市|