C
C#ā€¢13mo ago
SWEETPONY

How to optimize this?

I have following:
using System;

namespace SquareCalculus
{
internal class FigureTriangle : IFigure
{
private readonly double _sideA;
private readonly double _sideB;
private readonly double _sideC;

public FigureTriangle(
double sideA,
double sideB,
double sideC)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideA, nameof(sideA));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideB, nameof(sideB));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideC, nameof(sideC));

_sideA = sideA;
_sideB = sideB;
_sideC = sideC;
}

public double CalculateSquare()
{
var perimetr = (_sideA + _sideB + _sideC) / 2;

var square = Math.Sqrt(perimetr
* (perimetr - _sideA)
* (perimetr - _sideB)
* (perimetr - _sideC));

return square;
}

public bool IsRightTriangle()
{
var sides = new double[] { _sideA, _sideB, _sideC };

Array.Sort(sides);

return Math.Pow(sides[2], 2) == Math.Pow(sides[0], 2) + Math.Pow(sides[1], 2);
}
}
}
using System;

namespace SquareCalculus
{
internal class FigureTriangle : IFigure
{
private readonly double _sideA;
private readonly double _sideB;
private readonly double _sideC;

public FigureTriangle(
double sideA,
double sideB,
double sideC)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideA, nameof(sideA));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideB, nameof(sideB));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sideC, nameof(sideC));

_sideA = sideA;
_sideB = sideB;
_sideC = sideC;
}

public double CalculateSquare()
{
var perimetr = (_sideA + _sideB + _sideC) / 2;

var square = Math.Sqrt(perimetr
* (perimetr - _sideA)
* (perimetr - _sideB)
* (perimetr - _sideC));

return square;
}

public bool IsRightTriangle()
{
var sides = new double[] { _sideA, _sideB, _sideC };

Array.Sort(sides);

return Math.Pow(sides[2], 2) == Math.Pow(sides[0], 2) + Math.Pow(sides[1], 2);
}
}
}
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
dan in a can
dan in a canā€¢13mo ago
since all the side variables are class level anyway, why not make sides class level?
x0rld šŸ‘» šŸŽƒ
the sides + sort can be made in the ctor also
cap5lut
cap5lutā€¢13mo ago
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 struct
SWEETPONY
SWEETPONYOPā€¢13mo ago
thanks for advices maybe Im wrong but can Lazy help me?
cap5lut
cap5lutā€¢13mo ago
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?
SWEETPONY
SWEETPONYOPā€¢13mo ago
hm no but it is a test task from company and Iā€™d like to write as good as I can
cap5lut
cap5lutā€¢13mo ago
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
Want results from more Discord servers?
Add your server