本文介绍了给定一个整数,其varint编码的大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python整数列表,我想知道当编码为协议缓冲区序列的变长整数或变体.在不实际对整数进行编码的情况下解决此问题的最佳方法是什么?

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))  # ???

推荐答案

每个整数都以128为基数编码,每个数字"一个字节.以任何基数表示的整数值的长度为ceil(log(value,base)).

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)).

取每个整数的对数(以128为底);将这些值四舍五入到最接近的整数;将这些四舍五入的值相加,就可以了.

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.

这篇关于给定一个整数,其varint编码的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:34