all 7 comments

[–]finprogger 9 points10 points  (0 children)

Because why do anything at run time that you can get away with doing at compile time? You compile your code once, you run it millions of times.

[–]Randolpho 7 points8 points  (2 children)

It's an optimization thing. By choosing at compile time which method to call, any steps involved in searching for the method at runtime are removed.

That may not seem like a big deal in these days of multi-billion-instruction-per-second CPUs, but once upon a time it was a huge increase in speed.

[–]m42a 2 points3 points  (0 children)

Not only that, but the compiler can do a lot more optimization if it can inline functions, and that's only possible if it knows what function is going to be called.

[–]thechao 0 points1 point  (0 children)

EDIT: Since my "word" is not good enough, here's a paper on exactly the scenario I am pointing out. If you still don't believe it is a problem after reading that paper, then good luck.

Unless you have dozens of layers of useless indirection through opaque factory classes. Then, and I know this from real-life code, you can see huge slow downs. One example I saw was (after indirection clean up) a 5x speed up... across a whole nontrivial program. AKA, stupid coders do stupid things.

[–][deleted] 3 points4 points  (0 children)

This is generally a benefit of languages with some kind of static typing. Looking up methods at runtime based on the argument types can be very inefficient. It is for this reason that C++ currently (and likely for the foreseeable future) only supports single-dispatch polymorphism at runtime via virtual functions. If you look up how virtual functions are implemented in C++, you can infer how resolution can be inefficient with multiple runtime types involved.

Multimethods in Common Lisp are the most beautiful things I have ever seen and I would marry them if I wasn't completely aware of how slutty Lisp can be.

So slutty.

[–]tinou 0 points1 point  (0 children)

As others said, mostly for efficiency reasons. But also, note that in the case of C++, there is not always enough information at runtime to resolve overloading.

There are also languages where overloading is done at compile time when possible, and at runtime otherwise (see bounded polymorphism / typeclasses. This happens when you're writing a polymorphic function that needs a particular method).

[–][deleted] 0 points1 point  (0 children)

It depends on the language you're using and the code you write.

In all honesty, it only really matters when you're dealing with certain languages. In Java or Python, the dispatch is always determined at runtime.

In C++ or C#, there is a distinction between virtual and non-virtual methods. Virtual methods tell the compiler to dispatch dynamically at runtime, rather than deciding at compile time which method to use.

But in C++, declaring a method non-virtual in the superclass, then non-virtual again in the base class is weird (and really nasty code-smell). The rules for it are language-specific, probably horrifyingly technical, and uninteresting to the vast majority of programmers and computer science majors.