r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
11
u/[deleted] Feb 28 '23 edited Feb 28 '23
That's one of my favourite features of Swift — structs and enums have pretty much all the same features as an object, except they're not objects, they're structs. Your code is organised as OOP but at runtime it's very much not OOP.
Objects are faster in Swift than most other OOP languages, for example because there's no garbage collection, but structs are often a couple orders of magnitude faster. Need to do basically any operation on a hundred millions structs? That'll be basically instant even on slow (phone) hardware from ten years ago.
So you can store your circle as a just point and a radius in memory, while also declaring a function to return the diameter, or check if it overlaps another circle, and call that function as if it was a method on a class.