Tomm’s Tinkerings



Clash of the unlikely: C# versus.. RealBASIC?

As an Opensim developer, I use C# with .NET or Mono on a daily basis. To be honest, I love the language. It’s clean, efficient and simple.

Yesterday however, I suggested on IRC that whilst C# has definately benefited OpenSim with its simple debugging, rapid development and wide selection of prebuilt classes to choose from, it was not a great choice for what is, essentially, a high performance application.

Upon stating this, a couple of people chirped up to tell me that while a language like C or C++ would provide better performance, I would be suprised how far JIT compilers have come.

I decided to put this to the test. Hopefully, I supposed, I would be proven wrong and the performance of C# would very much impress me. But I didn’t want to prove what had already been proven (that is, C and C++ will outperform C# every time), but I wanted to pitch a performance comparison against an unlikely competitor, one which most definately isn’t seen as a “professional” language and hardly ever considered for real applications.

Enter RealBASIC. This commercial product from RealSoftware contains a vast featureset of prebuilt classes and modules, very similar to .NET, and its rapid compile system will spit out native code for Mac, Linux and Windows in one swoop. Of course, it’s not an open specification (which is why it’s not considered for serious uses) and the IDE can set you back a very steep €400.

Why did I choose RealBASIC? Because it’s a development environment with a lot of overhead, BASIC is widely considered as being extremely slow, and it has built in functionality very similar to that available with .NET.

So, I decided to write a simple test to guage the performance between the two systems. Randomly generate one million UUID’s and place them into a dictionary, first performing a lookup to ensure there are no collisions.

You can see the C# code here

You can see the RealBASIC code here

All tests were performed consecutively, alternating between C# and RealBASIC. Both were with “release” executables, without debug info. Both were targeted at x86. I don’t think I could have predicted the result.

C#
40.971 seconds, 1,000,000 keys in dictionary (0 collisions)
39.773 seconds, 1,000,000 keys in dictionary (0 collisions)
42.466 seconds, 1,000,000 keys in dictionary (0 collisions)

RealBASIC
25.876 seconds, 1,000,000 keys in dictionary (0 collisions)
25.425 seconds, 1,000,000 keys in dictionary (0 collisions)
25.555 seconds, 1,000,000 keys in dictionary (0 collisions)

I think that these results just go to show that JIT compiled languages have a long way to go yet, before they are really appropriate for high performance applications.

I would like to point out that I am NOT suggesting that anybody use RealBASIC, i’m not suggesting that Opensim should have been coded in RealBASIC, it just seemed like a fair comparison to me. I do think that C# is a powerful language, and .net is a decent platform, and there are far more evil languages out there. If we were to perform this test on Java, these results would have been in the hundreds.

~T

Advertisement

Comments

  1. Miguel de Icaza says:

    What we have found is that applications that are performance sensitive spend the majority of their time on a tiny part of the application.

    What has worked great for our users is to identify the slow parts of the code with a profiler and if required rewrite those pieces in C or even assembler and just P/Invoke that code

    | Reply Posted 2 years, 10 months ago
  2. Adam Frisby says:

    There’s a couple of points in your C# code which will cause noticable slowdowns.

    Strings for instance are immutable types – a+b = c. rather than modify a or b’s buffer. Doing 16x string concats would be fairly expensive – so the C# runtime includes the StringBuilder class to make string concatenation faster.

    I would guess that one change would probably give you a pretty healthy performance boost.

    The second major slowdown point is your Chr function – you are re-requesting a very specific encoding, which has to be looked up by the runtime, and doing so 16 million times.

    I would switch to either Encoding.UTF8.GetChars which is a static constant.

    | Reply Posted 2 years, 10 months ago
  3. Adam Frisby says:

    Appending to the above – make sure to test on both Mono and .NET. Mono traditionally gets pretty bad performance by comparison.

    | Reply Posted 2 years, 10 months ago


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.