Re: C#: abstract classes VS interfaces

I would be very surprised if interfaces are slower than abstract classes by enough to matter.  At the end of the day they're both about the same thing once you start passing them around by reference, namely a couple of pointers.

.net uses interfaces very heavily in the libraries that come with it.  If it's fast enough for them it's fast enough for you.  use the best abstraction for the job, then make it faster if it happens to matter later.  Maintaining code is already hard enough.  Foregoing good code at the beginning for the sake of something which might be faster just makes your life unnecessarily harder.

But that's not the most important point.  The most important point is that C# is a just-in-time compiler, which means it's doing rather a lot of magic at runtime to try to "see through" things that might be slow.  The net result is that while people can go out of their way to make things that i.e. show that abstract classes are slower than interfaces, it's also almost certainly possible to go equally out of your way to make something that shows that interfaces are faster than abstract classes.  In order to actually make this determination you have to have specific code to time that applies directly to your problem, then play with it.  And even once you've done that, Microsoft might release an update that changes the JIT in some way that makes your micro-optimization a slower way than if you hadn't.

Rule 1 of the modern compiler, and also the modern just-in-time compiled language, is that the simple code is usually the fastest.  Rule 2 is that if the simplest code isn't the fastest today, it will be in a year or two when whoever is responsible for maintaining it throws their army of coders who probably all have PHDs at fixing it.

You get way more mileage out of using a better sorting algorithm or something else of that ilk than worrying whether or not using an interface helps or hurts.  In practice, the only really good way to think about performance in the modern programming environment is to think in something called algorithmic complexity, but as to not get into some really deep stuff, a better sorting algorithm can be literally a million times faster, whereas an interface vs an abstract class is going to be less than 2 times difference (and I'd put money on it being zero).

Maintainability gets you to the finish line. Optimizing before you have something that needs optimizing doesn't get you to the finish line, and instead sends you down the road of learning lots of cool but ultimately useless stuff that doesn't get you a project.  The latter can be quite enjoyable and informative but matters little in practice.  Which you want to do now is up to you.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Nuno via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Nuno via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Rastislav Kiss via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Rastislav Kiss via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Nuno via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : leibylucw via Audiogames-reflector

Reply via email to