Alright, that’s not quite fair, but anyone that assumes that C# is going to be slow just because Java is making a big mistake. One of my classmates made this mistake, and it should have cost him $5, but I chickened out and wouldn’t take the bet. Anyway:
We had a programming assignment to find the average number of colors to color a graph. I won’t go into the details, but suffice to say that we had very similar solutions in terms of algorithmic design (in theory my algorithm should have been faster, but it looks like its advantage is minimal in practice), and he thought his program would smoke mine due to our choices in language and platform. I chose C# and .NET 3.5 running on Windows, he chose straight-up C running on MacOS. We both ran our programs on MacBook’s with identical processors. Even *I* expected the C program to win out. When I use .NET, I know that I am giving up some performance in exchange for an improved development experience… or so I thought.
We fired up our programs in dramatic fashion, then waited for them to complete. 10 minutes later, we had our winner: C# had completed the task ahead of C. The C program wrapped up around the 14 minute mark. I love C# and .NET, and though I expected C# to be close to C, I never expected it to be faster.
Despite the results, I still think the C version should have been faster; there must be some minor algorithmic, implementation, or environment difference that is causing the results. Still, this does demonstrate that C# is no slouch on the performance front.
Oh, and for anyone that likes Java: I talked to people who implemented the same assignment using Java, and their programs took *hours* to run for the same problem size. Suck on that, Java! 😛
thats probably because of memory allocation. i guess that your algorithms allocate many objects because your dealing with graphs. the .net allocator is 50 times faster that that of c++, i benchmarked it myself.
I actually had the same thought at first. Like you, I had already seen that C# was far better at allocating memory than C/C++. But our algorithms use very little memory and don’t make all that many allocations. His version allocates a single 64-bit int, mine allocates an NxN matrix of bools. My version is actually doing more memory allocations than his since I’m allocating a new NxN matrix for each graph; his uses the same 64-bit int for each subsequent graph.
What were their respective memory footprints?
Mine stayed around the 40Mb mark, I’m not sure what his was at, but like I said, his wasn’t allocating any memory. It used a single 64-bit int to represent each graph.