Guest User

Untitled

a guest
May 24th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <msgpack.hpp>
  2. #include <iostream>
  3. #include <sys/time.h>
  4. #include <vector>
  5.  
  6. struct simple_timer {
  7. void reset() { gettimeofday(&m_timeval, NULL); }
  8. void show()
  9. {
  10. struct timeval endtime;
  11. gettimeofday(&endtime, NULL);
  12. double sec = (endtime.tv_sec - m_timeval.tv_sec)
  13. + (double)(endtime.tv_usec - m_timeval.tv_usec) / 1000 / 1000;
  14. std::cout << sec << " sec" << std::endl;
  15. }
  16. private:
  17. struct timeval m_timeval;
  18. };
  19.  
  20.  
  21. struct insufficient_buffer_error : public std::runtime_error {
  22. insufficient_buffer_error() :
  23. std::runtime_error("buffer is insufficient") { }
  24. };
  25.  
  26. #define MORE(N) if(m_remain < N) { throw insufficient_buffer_error(); }
  27. #define ADVANCE(N) m_pos += N; m_remain -= N;
  28.  
  29. class unpacker2 {
  30. public:
  31.  
  32. inline uint64_t unpack_uint64()
  33. {
  34. uint64_t v;
  35.  
  36. MORE(1);
  37. if(m_pos[0] <= 0x7f) {
  38. // positivie fixnum
  39. v = *(uint8_t*)m_pos;
  40. ADVANCE(1);
  41.  
  42. } else if(m_pos[0] == 0xcc) {
  43. // unsigned int 8
  44. MORE(2);
  45. v = *(uint8_t*)(m_pos+1);
  46. ADVANCE(2);
  47.  
  48. } else if(m_pos[0] == 0xcd) {
  49. // unsigned int 16
  50. MORE(3);
  51. v = _msgpack_be16( *(uint16_t*)(m_pos+1) );
  52. ADVANCE(3);
  53.  
  54. } else if(m_pos[0] == 0xce) {
  55. // unsigned int 32
  56. MORE(5);
  57. v = _msgpack_be32( *(uint32_t*)(m_pos+1) );
  58. ADVANCE(5);
  59.  
  60. } else if(m_pos[0] == 0xcf) {
  61. // unsigned int 64
  62. MORE(9);
  63. v = _msgpack_be64( *(uint64_t*)(m_pos+1) );
  64. ADVANCE(9);
  65.  
  66. } else {
  67. throw msgpack::type_error();
  68. }
  69.  
  70. return v;
  71. }
  72.  
  73. inline uint32_t unpack_array()
  74. {
  75. uint32_t len;
  76.  
  77. MORE(1);
  78. if(0x0a <= m_pos[0] && m_pos[0] <= 0xbf) {
  79. // fixarray
  80. len = ((unsigned int)m_pos[0]) & 0x0f;
  81. ADVANCE(1);
  82.  
  83. } else if(m_pos[0] == 0xdc) {
  84. // array 16
  85. MORE(3);
  86. len = _msgpack_be16( *(uint16_t*)(m_pos+1) );
  87. ADVANCE(3);
  88.  
  89. } else if(m_pos[0] == 0xdd) {
  90. // array 32
  91. MORE(5);
  92. len = _msgpack_be32( *(uint32_t*)(m_pos+1) );
  93. ADVANCE(5);
  94. }
  95.  
  96. return len;
  97. }
  98.  
  99. public:
  100. unsigned char* m_buffer;
  101. unsigned char* m_pos;
  102. size_t m_remain;
  103. };
  104.  
  105. int main(int argc, char* argv[])
  106. {
  107. simple_timer timer;
  108.  
  109. msgpack::sbuffer sbuf;
  110. msgpack::packer<msgpack::sbuffer> pk(sbuf);
  111.  
  112. const size_t loop = atoi(argv[1]);
  113.  
  114. pk.pack_array(loop);
  115. for(size_t i=0; i < loop; ++i) {
  116. msgpack::pack(sbuf, i);
  117. }
  118.  
  119. // static unpacker
  120. timer.reset();
  121. {
  122. unpacker2 pac;
  123. pac.m_buffer = (unsigned char*)sbuf.data();
  124. pac.m_pos = pac.m_buffer;
  125. pac.m_remain = sbuf.size();
  126.  
  127. std::vector<uint64_t> result;
  128.  
  129. uint32_t len = pac.unpack_array();
  130. result.reserve(len);
  131. for(size_t i=0; i < len; ++i) {
  132. uint64_t v = pac.unpack_uint64();
  133. result.push_back(v);
  134. }
  135. }
  136. timer.show();
  137.  
  138. // dynamic unpacker + type conversion
  139. timer.reset();
  140. {
  141. msgpack::zone z;
  142. msgpack::object obj;
  143.  
  144. msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
  145.  
  146. std::vector<uint64_t> result;
  147. obj.convert(&result);
  148. }
  149. timer.show();
  150. }
  151.  
  152.  
  153. // $ ./a.out 10000
  154. // 0.000489 sec
  155. // 0.000974 sec
  156.  
  157. // $ ./a.out 1000000
  158. // 0.040277 sec
  159. // 0.084064 sec
  160.  
  161. // Mac OS X Snow Leopard
  162. // Intel Core 2 Duo 2.53 GHz
  163. // DDR3 1067MHz 8GB
Add Comment
Please, Sign In to add comment