zoukankan      html  css  js  c++  java
  • msgpack[C++]使用笔记 和 msgpack/cPickle性能对比

    python版本msgpack安装:

    wget http://pypi.python.org/packages/source/m/msgpack-python/msgpack-python-0.1.9.tar.gz

    python2.x setup.py install --prefix=/usr/local/similarlib/


    python版本的msgpack灰常好用,速度上比python内置的pickle和cpickle都要快一些,C++版本的使用比较麻烦,下面是本人学习时的一个demo,解析python-msgpack dump的一个复杂字典

    1. #include <msgpack.hpp>  
    2.   
    3. #include <fstream>  
    4. #include <iostream>  
    5. using namespace std;  
    6.   
    7. template <class T>  
    8. void msgunpack(const char* binary_file, T& t, char* buff, uint32_t max){  
    9.     msgpack::unpacked msg;  
    10.     ifstream tf_file(binary_file,ios::in|ios::binary|ios::ate);  
    11.     uint32_t size = tf_file.tellg();  
    12.     tf_file.seekg(0, ios::beg);  
    13.     tf_file.read(buff, size);  
    14.     tf_file.close();  
    15.     msgpack::unpack(&msg, buff, size);  
    16.     msg.get().convert(&t);  
    17. }  
    18.   
    19.   
    20. typedef map<uint32_t, uint32_t> WordsMap;  
    21. typedef map<uint32_t, WordsMap> FieldsMap;  
    22. typedef map<uint64_t, FieldsMap> DocsMap;  
    23.   
    24. int main(int argc, char** argv)  
    25. {  
    26.     uint32_t MAX_BUFF = 1024*1024*100; //100MB  
    27.     char* BUFF = new char[MAX_BUFF];  
    28.   
    29.     DocsMap docsMap;  
    30.     msgpack::unpacked msg;  
    31.     msgunpack("/data/wikidoc/tf_dict_for_nodes/1-1000", docsMap, BUFF, MAX_BUFF);  
    32.     //        msg.get().convert(&docsMap);  
    33.     cout << docsMap.size() << endl;  
    34.         delete[] BUFF;  
    35. }  



    参考: http://wiki.msgpack.org/pages/viewpage.action?pageId=1081387#QuickStartforC%2B%2B-ImplementationStatus


    下面是本人自己封装的一个msgpack接口头文件mymsgpack.h

    1.  #ifndef MY_MSGPACK_H  
    2.   
    3. #ifndef MY_MSGPACK_H  
    4. #define MY_MSGPACK_H  
    5. #include <fstream>  
    6. #include <msgpack.hpp>  
    7. using namespace std;  
    8.   
    9. template <class T>  
    10. void load_from_file(const char* binary_file, T& t) {  
    11.         ifstream binaryFstream(binary_file,ios::in|ios::binary|ios::ate);  
    12.         uint32_t size = binaryFstream.tellg();  
    13.         char* buff = new char[size];  
    14.         binaryFstream.seekg(0, ios::beg);  
    15.         binaryFstream.read(buff, size);  
    16.         binaryFstream.close();  
    17.         msgpack::unpacked msg;  
    18.         msgpack::unpack(&msg, buff, size);  
    19.         msg.get().convert(&t);  
    20.         delete[] buff;  
    21. }  
    22.   
    23. template <class T>  
    24. void load_from_str(const char* binary_str, int len, T& t) {  
    25.         msgpack::unpacked msg;  
    26.         msgpack::unpack(&msg, binary_str, len);  
    27.         msg.get().convert(&t);  
    28. }  
    29.   
    30. template <class T>  
    31. void dump_to_file(T& t, const char* dump_file) {  
    32.     msgpack::sbuffer sbuf;  
    33.     msgpack::pack(sbuf, t);  
    34.     ofstream dumpFstream(dump_file, ios::out|ios::binary|ios::trunc);  
    35.     dumpFstream.write(sbuf.data(), sbuf.size());  
    36.     dumpFstream.close();  
    37. }  
    38.   
    39. template <class T>  
    40. void dump_to_str(T& t, char** dump_str, int& len) { //外部释放*dump_str  
    41.     msgpack::sbuffer sbuf;  
    42.     msgpack::pack(sbuf, t);  
    43.     len = sbuf.size();  
    44.     *dump_str = (char*)malloc(sbuf.size());  
    45.     memcpy(*dump_str, sbuf.data(), sbuf.size());  
    46. }  
    47.   
    48. #endif  


     


    msgpack编译通过,链接不上的问题 undefined reference to `__sync_sub_and_fetch_4'

    在x84_64机器上正常,在32bit机器上出现上述问题

    [plain] view plaincopy
    1. [xudongsong@BigServerU-4 msgpack-0.5.7]$ cat /etc/issue  
    2. CentOS release 5.4 (Final)  
    3. Kernel   on an m  
    4.   
    5. [xudongsong@BigServerU-4 msgpack-0.5.7]$ file /sbin/init  
    6. /sbin/init: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped  

    ./configure不报错,但是查看config.log显示有错误,程序链接msgpack的库时也报错

    原因:gcc不能识别CPU体系,需要手动指明

    [plain] view plaincopy
    1. [xudongsong@BigServerU-4 msgpack-0.5.7]$ CFLAGS="-march=pentium -mtune=pentium" ./configure --prefix=/home/xudongsong/msgpack_static --enable-static=yes --enable-shared=no  

    make, make install

    [xudongsong@BigServerU-4 jobs]$ g++ job_calc_weight.cpp -o job_calc_weight -I/home/xudongsong/msgpack_static/include/ -L/home/xudongsong/msgpack_static/lib/ -lmsgpack

    通过!

     


     

    下面是msgpack和cPickle进行性能pk的demo程序(不比较pickle,是因为它比cPickle更慢,《Python cook book》里面有说明):

    [python] view plaincopy
    1. mport sys,time,msgpack,pickle,cPickle,random  
    2.   
    3. test_list = []  
    4. i = 0  
    5. while i<100000:  
    6.     test_list = random.randrange(1,100000)  
    7.     i += 1  
    8.   
    9. print "common len(serialize) = %s"%len(cPickle.dumps(test_list,0))  
    10. print "compress len(serialize) = %s"%len(cPickle.dumps(test_list,1))  
    11.   
    12. #------------------------------------------------------------------------  
    13. results = {}  
    14. time_start = time.time()  
    15. for i in range(1,1000000):  
    16.         results[i] = cPickle.dumps(test_list,1)  
    17. time_mid_1 = time.time()  
    18. print "cPickle dumps eats %s s"%str(time_mid_1-time_start)  
    19.   
    20. for i in range(1,1000000):  
    21.     cPickle.loads(results[i])  
    22. time_mid_2 = time.time()  
    23. print "cPickle loads eats %s s"%str(time_mid_2-time_mid_1)  
    24.   
    25. #------------------------------------------------------------------------  
    26. results = {}  
    27. time_start = time.time()  
    28. for i in range(1,1000000):  
    29.     results[i] = msgpack.dumps(test_list)  
    30. time_mid_1 = time.time()  
    31. print "msgpack pack eats %s s"%str(time_mid_1-time_start)  
    32.   
    33. for i in range(1,1000000):  
    34.     msgpack.loads(results[i])  
    35. time_mid_2 = time.time()  
    36. print "msgpack unpack eats %s s"%str(time_mid_2-time_mid_1)  
  • 相关阅读:
    SSM应用(五)--参数绑定
    SSM应用(四)--SpringMVC入门
    Oracle常用函数
    SSM应用(三)--Spring整合MyBatis
    SSM应用(二)--注解的使用
    SSM应用(一)--Spring入门
    MyBatis学习(十四)--逆向工程使用
    MyBatis学习(十三)--缓存
    MyBatis学习(十二)--懒加载
    MySQL常用函数
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318703.html
Copyright © 2011-2022 走看看