project-hatohol/hatohol — pull request #427 by kou (2014-08-21 07:20:04+00:00)
Title: [server][benchmark] Add benchmark for string join case

We often say "for performance" without benchmark. It is not good as a
programmer. We should discuss about performance based on fact. :-)

This commit adds a benchmark that includes benchmark framework
prototype. If we will use fact based discussion about "for performance",
I can extract the benchmark framework into server/mlpl/. We will be able
to create a new benchmark when we have benchmark framework. It helps us
discussing based on fact.

The benchmark is for the following cases:

Precondition:

std::list elements;
elements.push_back("A");
elements.push_back("B");
elements.push_back("C");
elements.push_back("D");
elements.push_back("E");

Case1:

string all;
all += join(", ", elements);
all += join(", ", elements);
all += join(", ", elements);

Case2:

string all;
SeparatorInjector injector(", ");
for (StringListIterator it = m_elements.begin();
it != m_elements.end();
++it) {
injector(all);
std::string &element = *it;
all += element;
}
injector.clear();
for (StringListIterator it = m_elements.begin();
it != m_elements.end();
++it) {
injector(all);
std::string &element = *it;
all += element;
}
injector.clear();
for (StringListIterator it = m_elements.begin();
it != m_elements.end();
++it) {
injector(all);
std::string &element = *it;
all += element;
}

Here is a result on my environment:

My environment:

* CPU: Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz
* configure command line: ./configure --prefix=/tmp/local
* CXXFLAGS: -g -O2 -O2 -g3

Result:

% make run-bench-string-join
./bench-string-join
Label: Total Average Median
string::join: (0.013s) (0.001ms) (0.001ms)
SeparatorInjector: (0.007s) (0.671us) (0.001ms)

The join approach is slower 0.5us than the SeparatorInjector approach.