简体   繁体   English

Google Protobuf基于C ++的python实现的性能

[英]Performance of C++ based python implementation of Google Protobuf

I am using Google Protobuf in my Python application. 我在Python应用程序中使用Google Protobuf。 Experimenting with the protobufs I found that Protobuf Message Creation is much slower in CPP based python implementation as compared to Python based Python implementation. 通过实验protobuf,我发现与基于Python的Python实现相比,在基于CPP的python实现中Protobuf消息创建要慢得多。

Message creation with PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp was upto 2-3 times slower as compared to pure python based Protobuf Message Creation. 与基于纯python的Protobuf消息创建相比,使用PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION = cpp的消息创建要慢2-3倍。

Is this expected? 这是预期的吗? I found that SerializeToString and ParseFromString both are faster in the cpp verison. 我发现SerializeToString和ParseFromString在cpp版本中都更快。 The difference in each case widens as the size of the Message increases. 随着消息大小的增加,每种情况下的差异都会扩大。

I am using python's standard timeit module to time these tasks. 我正在使用python的标准timeit模块来计时这些任务。

(Using Version 2.4.1 of google protobuf) (使用Google protobuf 2.4.1版)

Yes, I believe this is expected. 是的,我相信这是预期的。 The pure-Python implementation stores all the fields in a dict. 纯Python实现将所有字段存储在dict中。 To construct a new message, it essentially just creates an empty dict, which is very fast. 要构造一条新消息,它实际上只是创建一个空字典,这是非常快的。 The C++ implementation actually initializes a C++ DynamicMessage object under the hood and then wraps it. C ++实现实际上是在DynamicMessage初始化一个C ++ DynamicMessage对象,然后包装它。 DynamicMessage actually initializes all of the fields upfront, so even though it's implemented in C++, it's "slower" -- but this upfront initialization makes later operations faster. 实际上, DynamicMessage会预先初始化所有字段,因此,即使它是用C ++实现的,它也是“较慢”的-但是这种前期初始化使以后的操作更快。

I believe you can improve performance further by compiling C++ versions of your protobuf objects and loading them in as another extension. 我相信您可以通过编译protobuf对象的C ++版本并将其作为另一个扩展加载来进一步提高性能。 If I recall correctly, the C++-backed Python protobuf implementation will then automatically used the compiled versions rather than DynamicMessage . 如果我没记错的话,那么C ++支持的Python protobuf实现将自动使用编译后的版本,而不是DynamicMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM