Re: Benchmarks
On 6 Nov, 15:53, s0s...@gmail.co m wrote:
the above uses an rb tree (or equivalent) in the set class
[snip version using linear search]
this is a very important lesson. Print it out in big letters
and post it on your wall.
CAREFUL ALGORITH SELECTION CAN CREAM MICRO-OPTIMISATION
I may get this printed on a tee shirt
--
Nick Keighley
On 6 Nov, 15:53, s0s...@gmail.co m wrote:
The task: Write a program that reads a set of words from standard
input and prints the number of distinct words.
>
I came across a website that listed a few programs to accomplish this
task:http://unthought.net/c++/c_vs_c++.html(ignore all the language
flaming :-), and thought that all of them did unnecessary operations,
so I wrote my own. But for some reason, my version turned out slower
that ALL of the versions in the website, even though it seems to
perform less operations (yes, I benchmarked them on my own computer).
>
According to the website, the slowest version is:
>
#include <set>
#include <string>
#include <iostream>
>
int main(int argc, char **argv)
{
// Declare and Initialize some variables
std::string word;
std::set<std::s tringwordcount;
// Read words and insert in rb-tree
while (std::cin >word) wordcount.inser t(word);
// Print the result
std::cout << "Words: " << wordcount.size( ) << std::endl;
return 0;
>
}
input and prints the number of distinct words.
>
I came across a website that listed a few programs to accomplish this
task:http://unthought.net/c++/c_vs_c++.html(ignore all the language
flaming :-), and thought that all of them did unnecessary operations,
so I wrote my own. But for some reason, my version turned out slower
that ALL of the versions in the website, even though it seems to
perform less operations (yes, I benchmarked them on my own computer).
>
According to the website, the slowest version is:
>
#include <set>
#include <string>
#include <iostream>
>
int main(int argc, char **argv)
{
// Declare and Initialize some variables
std::string word;
std::set<std::s tringwordcount;
// Read words and insert in rb-tree
while (std::cin >word) wordcount.inser t(word);
// Print the result
std::cout << "Words: " << wordcount.size( ) << std::endl;
return 0;
>
}
My version is about 12 times slower than that. It uses lower-level
constructs. Here it is:
constructs. Here it is:
Any ideas as to what causes the big slowdown?
this is a very important lesson. Print it out in big letters
and post it on your wall.
CAREFUL ALGORITH SELECTION CAN CREAM MICRO-OPTIMISATION
I may get this printed on a tee shirt
--
Nick Keighley
Comment