ByteSize() 方法的复杂性

Complexity of ByteSize() method

c++ 协议缓冲区的方法 ByteSize() 的复杂度是多少?换句话说,是我设置参数时计算的大小,并存储在内部的协议缓冲区中,还是我调用ByteSize()时计算的。如果是后者,这对于大型协议缓冲区对象来说可能会非常昂贵,对吗?

答案似乎从其他函数的文档中得到了暗示:https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message

SpaceUsed() is noticeably slower than ByteSize(), as it is implemented using reflection (rather than the generated code implementation for ByteSize()). Like ByteSize(), its CPU time is linear in the number of fields defined for the proto.

ByteSizeLong() is generally linear in the number of fields defined for the proto.

我实际上并没有看到关于 ByteSize() 本身的复杂性的描述,只是这样:

int ByteSize() const

Legacy ByteSize() API.

所以它似乎建议您应该使用其他之一。