Benches on Bytes & Strings To Nums

agolangf · 2016-05-16 17:05:40 · 448 次点击    
这是一个分享于 2016-05-16 17:05:40 的资源,其中的信息可能已经有所发展或是发生改变。

I've been experimenting with different ways to encode/decode data and along the way ran the following benchmarks. I had the need to store all data types to a database as either a string or byte slice.

Benchmark_strToInt      20000000                72.0 ns/op
Benchmark_bytesToInt    50000000                24.4 ns/op
Benchmark_strToFloat    20000000               118 ns/op
Benchmark_bytesToFloat  50000000                23.9 ns/op
Benchmark_b64ToBytes     5000000               354 ns/op
Benchmark_gobNewDecoder  1000000              1583 ns/op  

A few observations:

  • You can store binary data, []byte, with json, but json encodes/decodes to/from base64
  • Converting []byte to int/float is very fast
  • Using gob requires creating a new encoder/decoder for every use

Benchmark Code


评论:

[deleted]:

Does the speed of encoding the data matter when you have database, network nd db driver overhead?

jayposs:

If the encoding or decoding is executed as part of every transaction, then I figured every little bit helps. I am currently working with boltdb which reads data really fast, so the decode/unmarshal process could certainly be a factor in the total time to process a request. In my situation a lot of data conversions are going on.

[deleted]:

Profiles or gtfo

jayposs:

I haven't used that acronym before, but will remember it in case the right situation comes along. Curious why you use a name that implies Haskell suxs.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

448 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传