Drastic changes in C# Runtimes

Hi All!

I answer a lot of questions in 2-3 languages each and writing the fastest code possible is my number one priority other than the obvious priority of writing correct code.. One language I always answer in is C#. I have a process to solving them:

  1. See if I can think of the optimal (or at least decent) solution first. Run it and note the runtime.
  2. If my solution does not beat 80% of other submissions I either stick with the algorithm and optimize it to the point it cannot be optimized any further, or rethink my approach and write a new algorithm with the same 80% rule.
  3. Profile the code and modify accordingly.
  4. Repeat until I have a fast solution.

Up until sometime in the last month I was able consistantly meet my 80% target using C# and more often than not exceed it. Suddenly it did not matter what I wrote I was always placing in the bottom 5%. I could optimize, try 3 different algorithms (optimizing each as far as possible) but it would make no difference, my runtime performance had suddenly turned absolutely horrible.

I did a couple of tests to see what was happening. I went back to my old solutions where I knew my runtime beat 100% of submissions, I would rerun that exact code only now I was placing in the bottom 5-10 percent and the identical code was taking anywhere from 2 to 5 times longer to complete! Then I started looking at the results of other peoples submissions. I would take the 2-3 fasted placed solutions copy/paste their code and test, same result... the code that holds the absolute speed record for a problem now only beats 5-10 percent of submissions. I tried this with MANY problems and the results were the same.

I can take one of my solutions in C# that beat 90%+ of subissions and now could only beat 5%, write the identical algorithm in C++ or Java and it would still place very well, although I have noticed the same slowdown in C++ but nowhere near as drastic.

Here is another odd part, I generally wrote memory effecient code but never bothered to prioritize that in C#, so my old solutions were never ranked incredibly well in memory usage, now suddenly they are beating 80-90% if submissions. So what was once incredible fast code with mediocre memory usage has now become incredible slow code with excellent memory usage and it is the identical code, line for line, character for character, comment for comment, 100% identical.

Is anyone else experiencing this? For me personally it is a real problem because the runtime measurements in C# have become completely meaningless and my main goal here is not so much learning new algorithms but working on my ability to write them as effeciently as possible in a variety of languages using the idioms and standard libraries of said languages. I now no longer have any meaningful feedback on their effeciency, Sure I can look at Time complexity but we all know O(n) = O(10n) = O(10000000n), just because the optimal solution to a problem has O(nlog(n)) time complexity and you write an algorithm that has O(nlog(n)) time complexity absolutely does not mean you have written fast code.

I don't know when it happened but the C# environment was C# 8 (I think) with .NET 6, it is now C# 12 with .NET 8 runtime. I haven't heard people on stack overflow or anywhere else complaining that MS dropped the ball and made .NET 8 2-3 time slower than .NET 6, so it seems to be a local problem here.

If anyone else is experiencing this or knows anything about it please let me know.

Thanks for taking the time to read all that!

EDIT:

I forgot to mention, C# runtimes were never incredibly consistent, one run would take 50ms and the next take 90ms, so I would typically run each version 5 times and consider the average to be close to true. Now when I do that, almost without exception each runtime is slower than the one before. It appears (at least on the surface) as though there is a penalization in the form of added milliseconds for submitting identical code back to back with the penalty size growing each time. This part may be coincidence I haven't tested enough to make a definite determination, but everything in the above post I have done enough of the be absolutely sure about.

Comments (2)