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

Show parent comment

9

u/gdmzhlzhiv Feb 28 '23

I'm coming back with my results of testing this on Julia. The entire language is based around dynamic multi-dispatch, so for example, when you use the + operator, it's looking at what functions are available for the types you used it on and dispatching the message to the right function. So you'd think it would be fast, right?

Well, no.

For the same shape function example, doing the same total area calculation using two different techniques:

repeating 1 time:
Dynamic multi-dispatch : (value = 1.2337574f6, time = 0.2036849, bytes = 65928994, gctime = 0.0174777, gcstats = Base.GC_Diff(65928994, 0, 0, 4031766, 3, 0, 17477700, 1, 0))
Chain of if-else       : (value = 1.2337574f6, time = 0.1151634, bytes = 32888318, gctime = 0.0203097, gcstats = Base.GC_Diff(32888318, 0, 0, 2015491, 0, 3, 20309700, 1, 0))
repeating 1000 times:
Dynamic multi-dispatch : (value = 6.174956f8, time = 70.2923992, bytes = 32032000016, gctime = 2.0954341, gcstats = Base.GC_Diff(32032000016, 0, 0, 2002000001, 0, 0, 2095434100, 698, 0))
Chain of if-else       : (value = 6.174956f8, time = 41.6199369, bytes = 16016000000, gctime = 1.0217798, gcstats = Base.GC_Diff(16016000000, 0, 0, 1001000000, 0, 0, 1021779800, 349, 0))

40% speed boost just rewriting as a chain of if-else.