tags:

views:

290

answers:

2

Hi,

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
+4  A: 

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.
csl
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.
pablo
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...)
Joe
+8  A: 
Crashworks
ooooops!!!! It did the trick: real 0m0.207suser 0m0.162ssys 0m0.044s
pablo