process_perf_results.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright John Maddock 2007.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <map>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <boost/regex.hpp>
  9. #include <boost/lexical_cast.hpp>
  10. #include <boost/format.hpp>
  11. #include <boost/math/special_functions.hpp>
  12. std::map<std::string, double> results;
  13. std::map<std::string, std::string> extra_text;
  14. void load_file(std::string& s, std::istream& is)
  15. {
  16. s.erase();
  17. if(is.bad()) return;
  18. s.reserve(is.rdbuf()->in_avail());
  19. char c;
  20. while(is.get(c))
  21. {
  22. if(s.capacity() == s.size())
  23. s.reserve(s.capacity() * 3);
  24. s.append(1, c);
  25. }
  26. }
  27. int main(int argc, const char* argv[])
  28. {
  29. //
  30. // Set any additional text that should accumpany specific results:
  31. //
  32. extra_text["msvc-dist-beta-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  33. extra_text["msvc-dist-nbinom-R-quantile"] = "[footnote The R library appears to use a linear-search strategy, that can perform very badly in a small number of pathological cases, but may or may not be more efficient in \"typical\" cases]";
  34. extra_text["gcc-4_3_2-dist-beta-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  35. extra_text["gcc-4_3_2-dist-nbinom-R-quantile"] = "[footnote The R library appears to use a linear-search strategy, that can perform very badly in a small number of pathological cases, but may or may not be more efficient in \"typical\" cases]";
  36. extra_text["msvc-dist-hypergeometric-cdf"] = "[footnote This result is somewhat misleading: for small values of the parameters there is virtually no difference between the two libraries, but for large values the Boost implementation is /much/ slower, albeit with much improved precision.]";
  37. extra_text["msvc-dist-nt-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  38. extra_text["msvc-dist-nchisq-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  39. extra_text["gcc-4_3_2-dist-hypergeometric-cdf"] = "[footnote This result is somewhat misleading: for small values of the parameters there is virtually no difference between the two libraries, but for large values the Boost implementation is /much/ slower, albeit with much improved precision.]";
  40. extra_text["gcc-4_3_2-dist-nt-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  41. extra_text["gcc-4_3_2-dist-nchisq-R-quantile"] = "[footnote There are a small number of our test cases where the R library fails to converge on a result: these tend to dominate the performance result.]";
  42. boost::regex e("^Testing\\s+(\\S+)\\s+(\\S+)");
  43. std::string f;
  44. for(int i = 1; i < argc-1; ++i)
  45. {
  46. std::ifstream is(argv[i]);
  47. load_file(f, is);
  48. boost::sregex_iterator a(f.begin(), f.end(), e), b;
  49. while(a != b)
  50. {
  51. results[(*a).str(1)] = boost::lexical_cast<double>((*a).str(2));
  52. ++a;
  53. }
  54. }
  55. //
  56. // Load quickbook file:
  57. //
  58. std::ifstream is(argv[argc-1]);
  59. std::string bak_file = std::string(argv[argc-1]).append(".bak");
  60. std::ofstream os(bak_file.c_str());
  61. e.assign(
  62. "\\[perf\\s+([^\\s.]+)"
  63. "(?:"
  64. "\\[[^\\]\\[]*"
  65. "(?:\\[[^\\]\\[]*\\][^\\]\\[]*)?"
  66. "\\]"
  67. "|[^\\]]"
  68. ")*\\]");
  69. std::string newfile;
  70. while(is.good())
  71. {
  72. std::getline(is, f);
  73. os << f << std::endl;
  74. boost::sregex_iterator i(f.begin(), f.end(), e), j;
  75. double min = (std::numeric_limits<double>::max)();
  76. while(i != j)
  77. {
  78. std::cout << (*i).str() << std::endl << (*i).str(1) << std::endl;
  79. std::string item = (*i).str(1);
  80. if(results.find(item) != results.end())
  81. {
  82. double r = results[item];
  83. if(r < min)
  84. min = r;
  85. }
  86. ++i;
  87. }
  88. //
  89. // Now perform the substitutions:
  90. //
  91. std::string newstr;
  92. std::string tail;
  93. i = boost::sregex_iterator(f.begin(), f.end(), e);
  94. while(i != j)
  95. {
  96. std::string item = (*i).str(1);
  97. newstr.append(i->prefix());
  98. if(results.find(item) != results.end())
  99. {
  100. double v = results[item];
  101. double r = v / min;
  102. newstr += std::string((*i)[0].first, (*i)[1].second);
  103. newstr += "..[para ";
  104. if(r < 1.01)
  105. newstr += "*";
  106. newstr += (boost::format("%.2f") % r).str();
  107. if(r < 1.01)
  108. newstr += "*";
  109. if(extra_text.find(item) != extra_text.end())
  110. {
  111. newstr += extra_text[item];
  112. }
  113. newstr += "][para (";
  114. newstr += (boost::format("%.3e") % results[item]).str();
  115. newstr += "s)]]";
  116. }
  117. else
  118. {
  119. newstr.append(i->str());
  120. std::cerr << "Item " << item << " not found!!" << std::endl;
  121. }
  122. tail = i->suffix();
  123. ++i;
  124. }
  125. if(newstr.size())
  126. newfile.append(newstr).append(tail);
  127. else
  128. newfile.append(f);
  129. newfile.append("\n");
  130. }
  131. is.close();
  132. std::ofstream ns(argv[argc-1]);
  133. ns << newfile;
  134. }