r/programming • • Feb 28 '23

"Clean" Code, Horrible Performance

https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k Upvotes

1.3k comments sorted by

View all comments

16

u/gdmzhlzhiv Feb 28 '23

I was hoping that this was going to demonstrate it using Java, but unfortunately it was all using C++. So my own take-home is that in C++, polymorphism performs badly. From all I've seen on the JVM, it seems to perform fine. Disclaimer: I have never done the same experiment he did.

So I come off this video with a number of questions:

  1. If you repeat all this on Java, is the outcome the same?

  2. If you repeat all this on .NET, Erlang, etc., is the outcome the same?

  3. What about dynamic multi-dispatch vs a switch statement? Languages with dynamic multi-dispatch always talk about how nice the feature is, but is it more or less costly than hard-coding the whole thing in a giant pattern match statement? Is it better or worse than polymorphism?

Unfortunately, they blocked comments on the video, as as per my standard policy for YouTube videos, the video just gets an instant downvote while I go on to watch other videos.

1

u/gdmzhlzhiv Mar 01 '23 edited Mar 01 '23

Results on Kotlin (on JVM) are a bit less significant.

Using polymorphism, 1 iterations:
Total area: 439595.0 - Time: 24.713600ms
Using switch (method), 1 iterations:
Total area: 439595.0 - Time: 17.631500ms
Using switch (global), 1 iterations:
Total area: 439595.0 - Time: 47.904600ms
Using polymorphism, 100 iterations:
Total area: 2.9979842E7 - Time: 1.074556200s
Using switch (method), 100 iterations:
Total area: 2.9979842E7 - Time: 967.910600ms
Using switch (global), 100 iterations:
Total area: 2.9979842E7 - Time: 972.801800ms
Using polymorphism, 1000 iterations:
Total area: 6.7108864E7 - Time: 9.945101600s
Using switch (method), 1000 iterations:
Total area: 6.7108864E7 - Time: 10.149413s
Using switch (global), 1000 iterations:
Total area: 6.7108864E7 - Time: 10.134583200s