speed_test.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2013-2015 KONDO Takatoshi
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
  10. // export LD_LIBRARY_PATH=path_to_boost_lib
  11. #include <msgpack.hpp>
  12. #include <string>
  13. #include <iostream>
  14. #include <sstream>
  15. #include <map>
  16. #include <boost/timer/timer.hpp>
  17. void test_map_pack_unpack() {
  18. std::cout << "[TEST][map_pack_unpack]" << std::endl;
  19. // setup
  20. std::cout << "Setting up map data..." << std::endl;
  21. std::map<int, int> m1;
  22. int const num = 30000000L;
  23. for (int i = 0; i < num; ++i) m1[i] = i;
  24. std::cout << "Start packing..." << std::endl;
  25. std::stringstream buffer;
  26. {
  27. boost::timer::cpu_timer timer;
  28. msgpack::pack(buffer, m1);
  29. std::string result = timer.format();
  30. std::cout << result << std::endl;
  31. }
  32. std::cout << "Pack finished..." << std::endl;
  33. buffer.seekg(0);
  34. std::string str(buffer.str());
  35. msgpack::object_handle oh;
  36. std::cout << "Start unpacking...by void unpack(object_handle& oh, const char* data, size_t len)" << std::endl;
  37. {
  38. boost::timer::cpu_timer timer;
  39. msgpack::unpack(oh, str.data(), str.size());
  40. std::string result = timer.format();
  41. std::cout << result << std::endl;
  42. }
  43. std::cout << "Unpack finished..." << std::endl;
  44. std::map<int, int> m2;
  45. std::cout << "Start converting..." << std::endl;
  46. {
  47. boost::timer::cpu_timer timer;
  48. oh.get().convert(m2);
  49. std::string result = timer.format();
  50. std::cout << result << std::endl;
  51. }
  52. std::cout << "Convert finished..." << std::endl;
  53. }
  54. int main(void)
  55. {
  56. test_map_pack_unpack();
  57. }