C
C#

How to optimize this?

How to optimize this?

Iivefifthsence11/20/2023
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?
DIAdan in a can11/20/2023
since all the side variables are class level anyway, why not make sides class level?
Xx0rld11/20/2023
the sides + sort can be made in the ctor also
Ccap5lut11/20/2023
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
Iivefifthsence11/20/2023
thanks for advices maybe Im wrong but can Lazy help me?
Ccap5lut11/20/2023
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?
Iivefifthsence11/20/2023
hm no but it is a test task from company and I’d like to write as good as I can
Ccap5lut11/20/2023
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

Looking for more? Join the community!

C
C#

How to optimize this?

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
In VSCode, SDK not Recognized on ChromeOSHi. I just installed the .NET SDK and the Runtime by following the instructions for Ubuntu on the MiNito.AsyncEx vs DotNext.ThreadingWe're currently searching for a nice AsyncAutoResetEvent implementation. We found two suitable impleSequential BlinkersHello everyone! I bought these sequential blinkers that run off an stm32 blue pill board. the only pTrying to use Microsoft.Kiota namespace but not found when importingI am trying to use this method from the Microsoft.Kiota.Abstractions.Extensions namespace: ToFirstChWinUI3 Scheduler CalendarViewhello im trying to make a winui3 calendarview interactive calendar where I can predefine dates in myThe call is ambiguous between the following methods or properties: 'Thread.Thread(ThreadStart)' andi have to upload by filesHelp a noobie out with a simple hangmanHi! New to coding and I need some help to solve this problem. Shall I save the words to a list? AnyC# Image Resizing on Visual Studio CodeI've tried both ``newPic.SizeMode = PictureBoxSizeMode.StretchImage;`` and ``newPic.SizeMode = PictuI'm stuck,visual studio 22, I need help making my invaders/enemies move left to right then downI’m trying to get my invaders/enemies to move left to right then down like in the game space invader✅ Where should I store all the sensitive file on .NET project?Hello everyone, May I know where should I store all the credential files that needs to be used by .Unity CodeI have been trying to get my C# code to work in unity for about a week and I am officially lost. I'vHelp! Reporting service in dotnet Core?please don't mind my english, it is not that good. i have created 2 projects one is server -dotnetCatAPI Json deserialization issue.I'm having trouble getting my code to deserialize my json correctly. When I run my Program.cs I get ✅ Any idea why I cant use sendkey?Authorization in microservices archHello everyone, I'm quite new to the NET microservices arhitecture and right now I'm implementing a.✅ I can't get my runButton button to work on a windows form app```cs if (e.KeyCode == Keys.Enter || (e.Modifiers == Keys.None && sender == runButton)) ``` I can sGit and Google Drivenew git user.. it seems to have your local repositories in a google drive controlled file structure ASP.NET CRUD MODALI created a MVC controller with actions (CRUD) and I would like to know, how to modify this pre geneError✅ using from another csprojCurrently i have 2 projects, an api app and a worker service app And both use the same models. Is th