0

I'm comparing the following code in C++ and C# and C# (Mono 2.4) seems to be faster. Is there anything wrong with the C++ code?

 #include <map>
 #include <string>
 #include <iostream>
 #include <ext/hash_map>
 #include <boost/any.hpp>

 int main()
 {
    //std::map<long, long> m;
    // hash_map is a little bit faster
    __gnu_cxx::hash_map<long, long> m;

    for( long i = 0; i < 1000000; ++i )
    {
        m[i]  = i;
    }

 }

And C#

 using System;
 using System.Collections;

 public int Main()
 {
     Hashtable m = new Hashtable();

     for( long i = 0; i < 1000000; ++i )
     {
        m[i]  = i;
     }

}

C# code is actually twice as fast on the same machine.

$ time ./a.out

real    0m1.028s
user    0m0.986s
sys     0m0.041s

$ time mono test.exe

real    0m0.603s
user    0m0.732s
sys     0m0.090s
8
  • 5
    Did you compile the C++ code with optimizations turned on? Commented Oct 27, 2009 at 23:19
  • Also note that initial hash map sizes may be different. E.g., if the C# is larger, and C++ version lower, you might easily see a big differences. Commented Oct 27, 2009 at 23:20
  • 1
    Have you tried Boost.Unordered? Commented Oct 27, 2009 at 23:20
  • 1
    What OS are you on? Is the C++ binary stripped? Is it statically or dynamically linking the a.out file. Commented Oct 27, 2009 at 23:23
  • Hi. I just compiled with gcc test.cc. No switches, nothing. Didn't try with the default sizes either, but the Mono/C# default size is not very big either. Didn't try Boost.Unordered. The old fedora I'm using doesn't seem to have it. I'll look at a newer box (ok, or I'll install it :-P). Commented Oct 27, 2009 at 23:23

2 Answers 2

8

You need to compile the C++ code with compiler optimizations turned on for a fair comparison. Otherwise, you are comparing apples to debug builds — the compiler will not even try to emit fast code.

In GCC this would be the -O3 flag, to start with.

Sign up to request clarification or add additional context in comments.

Comments

4

Some thoughts:

  • You're comparing Mono against Boost
  • Which optimization (default or not) settings did you use for Mono and C++ ?
  • What are the initial / default hash table sizes for the C# and C++ versions ?
  • Have you tried comparing against other C++ hash maps, such as the sparse hash table ?
  • Remember not only to look at real time, but actual CPU time used.

2 Comments

I'd like to know how much faster Boost is. Didn't enable any optimizations for Mono nor C++. Not sure about the table sizes, I'll have to check. Considering other hashtables is exactly what I'd like to do. Yep, but even real+user is better for Mono.
I'd be surprised if Boost is that much faster. I don't think there have been great improvements in hash_map implementations the last few years. (Btw, your Linux setup might have the std::tr1::unordered_map, which is another hashing container implementation...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.