How to optimize this?
I have following:
Imagine I will call
IsRightTriangle
twice with the same parameters. It is not optimal to do sorting etc again again.. So how can I fix it?7 Replies
since all the side variables are class level anyway, why not make
sides
class level?the sides + sort can be made in the ctor also
and no matter where, dont allocate an array for 3 elements to sort it.
use a
Span<T>
for CalculateSquare
u can probably also use SIMD, tho i guess u will need to benchmark if it really will have some influence
and it probably maybe be a readonly structthanks for advices
maybe Im wrong but can Lazy help me?
not really worth it, either precompute it or compute it every time
did u actually benchmark it and the results have shown that this is a bottle neck, or is this premature optimization btw?
hm no but it is a test task from company and Iād like to write as good as I can
in the end its a matter of if u need to preserve the order of the sides, or if u can sort them at construction
but all of this also sorta shouts that this can be a readonly struct