C# vs C++ Performance Part II

In my previous post I discussed some of the timings of c# vs c++ and what I found. Well, it seemed so preposterous to me that the performance of c# was so much worse that I had to redo the timing.

Here is what I found, indeed my first test was flawed in the timings but still right in the conclusion. Basically C# is not good at shift operators. Here is what I did this time around and where I went a little wrong on my previous tests.

I re-did the port of the Cactus Kev Poker Hand Evaluator into c++ including the Paul Senzee modification for speed. I added a wrapper so the dll could be pinvoked from C#. Then I ported the poker calculator over the C# leaving it as untouched as possible.

This is where I went wrong before, the code I was running against in C# had been rewritten to run in my little poker simulator. It was not optimal for competing at raw speed but it was very easy to use in my simulator. So it was not really a fair test.

I then went on to write a little test wrapper that compared the two poker calculators making the same calls to each in the same looping constructs etc… Here is what I found:

  C# C++
    pinvoke each ranking pinvoke one call
Cactus Kev 184ms 316ms 173ms
With Paul Senzee Modification 82ms 210ms 52ms

The Cactus Kev method does not use logical operators and the C++ version is only 6% faster than the C# version. However, the C++ version using the Paul Senzee modification with the chunk of logical operators is 37% faster in C++ than in C#. Thats quite a difference.
So, my conclusion; The bottleneck remains the logical operators which I do not understand. I really need to go through the generated MSIL to see what is going on under the covers.

In case you are curious, here are the various ports of the code. All credit goes to Cactus Kev and Paul Senzee, these are just the ports. This was all compiled in Visual Studio 2005, including the C++

C# Poker Calculator
C++ Poker Calculator
Timing Project

4 Responses to “C# vs C++ Performance Part II”

  1. Chris Says:

    Hi,

    Just wondering if your C# code could be used to calculate odds of winning pre-flop, post-flop, fourth and fifth street? My C# kung-fu isn’t the strongest, do you have any code kicking around that doesn’t something like that?

    Regards,

    Chris

    Not directly, but you could pretty easily do that. Just take all possible combinations and run through them.  I did something similar while playing with it.  The last chunk I worked on, I hooked into Poker Academy to create a poker bot.  Let me tell you Java to C# is no small feat.

    Mike

  2. ron Says:

    Mike, could you give me some instruction on how you hooked into poker academy, I am working on somthing similar with pacific poker, and I am using dll injection. How did you do it with poker academy, as so far i have not been doing to well! All I want to do is test my code and see if i am a better player and collect my stats. As a point of interest, I converted cactus kev’s into java, which was real fun, especially because you cant import the tables so you have to convert them to files and read them in to either hash tables or arrays, depending on how you feel when writing the code! I am also using some starting tables out of poker for dummies, which really ties you down, I just wonder how they will do over time as I tend to play a little looser than that - lol. Any hints on the ‘hooking into’ aspect would be cool.

    Many thanks, Ron

  3. Mike Benson Says:

    Ron, what I did was created a Java Bot that loaded a C++ dll. The C++ then interoped into .NET. Quite a pain to do but it worked. On occasion Poker Academy would spit out an error on loading the bot but never had an error during running.

    Here is the Java code PAJava.rar that loaded my C++ interop dll.  All my bots were written in C# so they were loaded by my C++ dll.  My bots maintained their own table state etc… that allowed me to play the game with less than perfect information.  So all my Java bot had to do was pass the information off to C++.

  4. bokeh Says:

    Hi all,

    I have written a 5, 6 and 7 card evaluator in PHP (a language certainly not known for it’s speed). It is the simplest evaluator I have ever seen and is made up of about 10 lines of very basic code. It takes 16 microseconds (not milliseconds) to score a 7 card hand.

    I have used it in my online Texas Hold’em probability calculator which can be seen at http://texas.holdem.poker.probability.cal.culator.org/

    My guess is in C++ this evaluator would beat any other for speed, but I don’t know C++.

    I’d love to get in touch with anyone interested in the code or interested in porting it to a CLI in C++.

    Anyone interested can email me at http://bokehman.com/email

Leave a Reply