Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //
  2. // Compare libyaml and libyaml-cpp parsers for speed.
  3. //
  4. // Compile with:
  5. // g++ -O run.cpp -lyaml-cpp -lyaml -o run
  6. //
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <getopt.h>
  10. #include <stdint.h>
  11. #include <sys/time.h>
  12. #include <yaml.h>
  13. #include <yaml-cpp/yaml.h>
  14.  
  15. //
  16. // Load YAML file using C library.
  17. //
  18. void load_yaml_c(const char *filename)
  19. {
  20. struct timeval t0, t1;
  21. gettimeofday(&t0, 0);
  22.  
  23. // Initialize parser.
  24. yaml_parser_t parser;
  25. if (!yaml_parser_initialize(&parser)) {
  26. printf("%s: Failed to initialize YAML parser!\n", filename);
  27. exit(-1);
  28. }
  29.  
  30. // Set input file.
  31. FILE *fh = fopen(filename, "r");
  32. if (! fh) {
  33. perror(filename);
  34. exit(-1);
  35. }
  36. yaml_parser_set_input_file(&parser, fh);
  37.  
  38. // Parse the document.
  39. yaml_document_t yaml_document;
  40. if (!yaml_parser_load(&parser, &yaml_document)) {
  41. fclose(fh);
  42. printf("%s: YAML parser error %d\n", filename, parser.error);
  43. exit(-1);
  44. }
  45. fclose(fh);
  46. yaml_parser_delete(&parser);
  47.  
  48. // Find root element.
  49. if (!yaml_document_get_root_node(&yaml_document)) {
  50. printf("%s: YAML file is empty\n", filename);
  51. yaml_document_delete(&yaml_document);
  52. exit(-1);
  53. }
  54.  
  55. // Delete loaded data.
  56. yaml_document_delete(&yaml_document);
  57.  
  58. gettimeofday(&t1, 0);
  59. uintmax_t usec = (t1.tv_usec - t0.tv_usec) +
  60. (t1.tv_sec - t0.tv_sec) * 1000000UL;
  61. printf("C elapsed time: %.3f seconds\n", usec / 1000000.0);
  62. }
  63.  
  64. //
  65. // Load YAML file using C++ library.
  66. //
  67. void load_yaml_cxx(const char *filename)
  68. {
  69. struct timeval t0, t1;
  70. gettimeofday(&t0, 0);
  71.  
  72. std::vector<YAML::Node> root = YAML::LoadAllFromFile(filename);
  73.  
  74. // Delete loaded data.
  75. root.clear();
  76.  
  77. gettimeofday(&t1, 0);
  78. uintmax_t usec = (t1.tv_usec - t0.tv_usec) +
  79. (t1.tv_sec - t0.tv_sec) * 1000000UL;
  80. printf("C++ elapsed time: %.3f seconds\n", usec / 1000000.0);
  81. }
  82.  
  83. void usage()
  84. {
  85. printf("Test YAML input\n");
  86. printf("Usage:\n");
  87. printf(" run input.yml\n");
  88. exit(-1);
  89. }
  90.  
  91. int main(int argc, char **argv)
  92. {
  93. for (;;) {
  94. switch (getopt(argc, argv, "")) {
  95. case EOF:
  96. break;
  97. default:
  98. usage();
  99. }
  100. break;
  101. }
  102. argc -= optind;
  103. argv += optind;
  104.  
  105. if (argc != 1)
  106. usage();
  107.  
  108. const char *filename = argv[0];
  109. printf("Load file \"%s\"\n", filename);
  110.  
  111. load_yaml_c(filename);
  112. load_yaml_cxx(filename);
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement