1 //  Copyright John Maddock 2007.
2 
3 //  Use, modification and distribution are subject to the
4 //  Boost Software License, Version 1.0. (See accompanying file
5 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <map>
8 #include <fstream>
9 #include <iostream>
10 #include <boost/regex.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include <boost/format.hpp>
13 #include <boost/math/special_functions.hpp>
14 
15 std::map<std::string, double> results;
16 
17 std::map<std::string, std::string> extra_text;
18 
load_file(std::string & s,std::istream & is)19 void load_file(std::string& s, std::istream& is)
20 {
21    s.erase();
22    if(is.bad()) return;
23    s.reserve(is.rdbuf()->in_avail());
24    char c;
25    while(is.get(c))
26    {
27       if(s.capacity() == s.size())
28          s.reserve(s.capacity() * 3);
29       s.append(1, c);
30    }
31 }
32 
main(int argc,const char * argv[])33 int main(int argc, const char* argv[])
34 {
35    //
36    // Set any additional text that should accumpany specific results:
37    //
38    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.]";
39    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]";
40    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.]";
41    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]";
42    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.]";
43    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.]";
44    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.]";
45    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.]";
46    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.]";
47    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.]";
48 
49    boost::regex e("^Testing\\s+(\\S+)\\s+(\\S+)");
50    std::string f;
51    for(int i = 1; i < argc-1; ++i)
52    {
53       std::ifstream is(argv[i]);
54       load_file(f, is);
55       boost::sregex_iterator a(f.begin(), f.end(), e), b;
56       while(a != b)
57       {
58          results[(*a).str(1)] = boost::lexical_cast<double>((*a).str(2));
59          ++a;
60       }
61    }
62    //
63    // Load quickbook file:
64    //
65    std::ifstream is(argv[argc-1]);
66    std::string bak_file = std::string(argv[argc-1]).append(".bak");
67    std::ofstream os(bak_file.c_str());
68    e.assign(
69       "\\[perf\\s+([^\\s.]+)"
70       "(?:"
71          "\\[[^\\]\\[]*"
72             "(?:\\[[^\\]\\[]*\\][^\\]\\[]*)?"
73          "\\]"
74          "|[^\\]]"
75       ")*\\]");
76    std::string newfile;
77    while(is.good())
78    {
79       std::getline(is, f);
80       os << f << std::endl;
81       boost::sregex_iterator i(f.begin(), f.end(), e), j;
82       double min = (std::numeric_limits<double>::max)();
83       while(i != j)
84       {
85          std::cout << (*i).str() << std::endl << (*i).str(1) << std::endl;
86          std::string item = (*i).str(1);
87          if(results.find(item) != results.end())
88          {
89             double r = results[item];
90             if(r < min)
91                min = r;
92          }
93          ++i;
94       }
95       //
96       // Now perform the substitutions:
97       //
98       std::string newstr;
99       std::string tail;
100       i = boost::sregex_iterator(f.begin(), f.end(), e);
101       while(i != j)
102       {
103          std::string item = (*i).str(1);
104          newstr.append(i->prefix());
105          if(results.find(item) != results.end())
106          {
107             double v = results[item];
108             double r = v / min;
109             newstr += std::string((*i)[0].first, (*i)[1].second);
110             newstr += "..[para ";
111             if(r < 1.01)
112                newstr += "*";
113             newstr += (boost::format("%.2f") % r).str();
114             if(r < 1.01)
115                newstr += "*";
116             if(extra_text.find(item) != extra_text.end())
117             {
118                newstr += extra_text[item];
119             }
120             newstr += "][para (";
121             newstr += (boost::format("%.3e") % results[item]).str();
122             newstr += "s)]]";
123          }
124          else
125          {
126             newstr.append(i->str());
127             std::cerr << "Item " << item << " not found!!" << std::endl;
128          }
129          tail = i->suffix();
130          ++i;
131       }
132       if(newstr.size())
133          newfile.append(newstr).append(tail);
134       else
135          newfile.append(f);
136       newfile.append("\n");
137    }
138    is.close();
139    std::ofstream ns(argv[argc-1]);
140    ns << newfile;
141 }
142 
143