Re: Benchmarks
On Nov 6, 11:22 am, Obnoxious User <OU@127.0.0.1> wrote:
On Thu, 06 Nov 2008 07:53:18 -0800, s0suk3 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, s=
o
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 les=
s
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 wo=
rd;
std::set<std::string> wordcount;
// Read words and insert in rb-tree
while (std::cin >> word) wordcount.insert(word); // Pri=
nt 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:
[snip]
So, since your version uses "lower-level constructs" you assume
it would be automatically faster?
Well, in this case it did seem to have a couple of advantages, like
for example, InsertWord() is given a pointer directly to the malloc()-
allocated string, which it simply copies into the .word member of the
struct; it doesn't need to allocate new memory and copy the string
from one place to the other, whereas std::set::insert() does need to
do make a copy.
Also, I'm not sure, but is the set's destructor invoked when main()
goes out of scope (causing memory cleanup)? (Currently the other
version has the DeleteSet() call commented-out.)
But, as others have pointed out, it's clear that the reason for the
difference in performance is the searching mechanism.
Sebastian