Given an integer, how big is its varint encoding?

五迷三道 提交于 2019-12-12 11:09:48

问题


I have a python list of integers, and I'd like to know how much space it will take up when encoded as a sequence of Protocol Buffers variable-length integers, or varints. What's the best way to figure this out without actually encoding the integers?

my_numbers = [20, 69, 500, 38987982344444, 420, 99, 1, 999]
e = MyCoolVarintArrayEncoder(my_numbers)
print(len(e))  # ???

回答1:


Each integer is encoded in base 128, one byte per "digit". The length of an integer value's representation in any base is ceil(log(value, base)).

Take the log(base=128) of each integer; round those values up to the nearest integer; sum those rounded values, and there's your length.



来源:https://stackoverflow.com/questions/51603478/given-an-integer-how-big-is-its-varint-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!