Boost文本序列化和二进制序列化的效率比较

文本序列化需要对二进制数据进行转换,还要加入分隔符,因此不仅效率低而且耗费更多的空间。写了一个小程序比较一下二者效率相差相差多少,发现还是蛮悬殊的。例子程序中对同一个对象序列化100万次,该对象包含几种常用的数据类型,除了比较速度,还比较二者耗费的空间大小。程序代码如下:
#include <iostream>
#include <string>
#include <sstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>

using namespace std;

class Test
{
public:

friend class boost::serialization::access;

Test(bool b, char ch, int i, double d, string str)
: m_bool(b), m_char(ch), m_int(i), m_double(d), m_str(str)
{
}

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
ar& m_bool;
ar& m_char;
ar& m_int;
ar& m_double;
ar& m_str;
    }

private:
bool m_bool;
char m_char;
int m_int;
double m_double;
string m_str;
};

int main()
{
Test test(true, 'm', 50, 17.89, "fuzhijie");

stringstream binary_sstream;
stringstream text_sstream;

long long begin, end;

int size;

//使用二进制的方式序列化
boost::archive::text_oarchive text_oa(text_sstream);
boost::archive::binary_oarchive binary_oa(binary_sstream);

begin = time(NULL);
for(int i = 0; i < 1000000; ++i)
{
text_oa << test;
}
end = time(NULL);

size = text_sstream.tellp() / (1024 * 1024);

cout << "text serialization seconds: " << end - begin << ", space: " << size << endl;

begin = time(NULL);
for(int i = 0; i < 1000000; ++i)
{
binary_oa << test;
}
end = time(NULL);

        //以MB为单位
size = binary_sstream.tellp() / (1024 * 1024);

cout << "binary serialization seconds: " << end - begin << ", space: " << size << endl;

return 0;
};
        连续测试三次,结果如下:
text serialization seconds: 37, space: 37
binary serialization seconds: 8, space: 24

text serialization seconds: 40, space: 37
binary serialization seconds: 8, space: 24

text serialization seconds: 37, space: 37
binary serialization seconds: 8, space: 24
        从结果可以看出,二者速度相差5倍左右,耗费空间大小相差1.5倍左右。

利用boost::serialization来实现C++ RMI  

先来个boost::serlization使用的例子


#include 
< boost / archive / text_oarchive.hpp >
#include 
< boost / archive / text_iarchive.hpp >
#include 
< boost / archive / xml_oarchive.hpp >
void  test_serialization()
{
    boost::archive::text_oarchive to(cout , boost::archive::no_header);
    
int  i  =   10 ;
    
string  s  =   " This is a test\n " ;
    to 
&  i;
    to 
&  s;

    ofstream f(
" test.xml " );
    boost::archive::xml_oarchive xo(f);
    xo 
&  BOOST_SERIALIZATION_NVP(i)  &  BOOST_SERIALIZATION_NVP(s);

    boost::archive::text_iarchive ti(cin , boost::archive::no_header);
    ti 
&  i  &  s;
    cout 
<< " i= " <<  i  <<  endl;
    cout 
<< " s= " <<  s  <<  endl;
}

假如我们可以将object序列化以后通过socket传过去,就可以实现RMI了。
当然这种方法只能是在C++程序之间传递。

今天在codeproject上面发现已经有人已经这样做了,原来早就有人跟我有一样的想法
看看作者封装的结果吧

server端:
#include  < RCF / RCF.hpp >  

RCF_BEGIN(I_Echo, 
" I_Echo " )
  RCF_METHOD_R1(std::
string , echo,  const  std:: string   & );
RCF_END(I_Echo);

class  Echo
{
public :
  std::
string  echo( const  std:: string   & msg) {  return  msg; }
};

int  main()
{
  
int  port  =   50001 ;
  RCF::RcfServer server(port);
  server.bind
< I_Echo, Echo > ();
  server.start();
  
return   0 ;
}

client端:

#include  < RCF / RCF.hpp >

RCF_BEGIN(I_Echo, 
" I_Echo " )
  RCF_METHOD_R1(std::
string , echo,  const  std:: string   & );
RCF_END(I_Echo);

int  main()
{
  std::cout 
<<  RcfClient < I_Echo > ( " localhost "
                               
50001 ).echo( " my message " );
  
return   0 ;
}

比较简洁,利用了boost强大的function,thread,serlization
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Boost 库提供了非常强大的序列化和反序列化功能,可以用来将对象转换为二进制数据,以及从二进制数据还原为对象。以下是一个简单的示例代码,展示了如何使用 Boost 库进行序列化和反序列化: ```cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> class MyClass { public: int x; std::string name; // 序列化函数 template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & x; ar & name; } }; int main() { // 创建对象 MyClass obj; obj.x = 42; obj.name = "Boost"; // 序列化对象到文件 std::ofstream ofs("data.txt"); boost::archive::text_oarchive oa(ofs); oa << obj; ofs.close(); // 从文件反序列化对象 MyClass restoredObj; std::ifstream ifs("data.txt"); boost::archive::text_iarchive ia(ifs); ia >> restoredObj; ifs.close(); // 输出反序列化后的对象内容 std::cout << "Restored Object: " << std::endl; std::cout << "x: " << restoredObj.x << std::endl; std::cout << "name: " << restoredObj.name << std::endl; return 0; } ``` 在上述代码中,我们定义了一个名为 `MyClass` 的类,并在其中添加了 `serialize` 函数,该函数用于定义对象的序列化方式。我们使用 Boost 库中的 `text_oarchive` 类将对象序列化到文件中,并使用 `text_iarchive` 类从文件中反序列化对象。最后,我们输出反序列化后的对象内容。 请确保在编译时链接了 Boost 库,并将其头文件路径添加到编译器的包含路径中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值