Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active October 26, 2017 21:42
Show Gist options
  • Select an option

  • Save musoftware/66502a6634f634b522c746a4c9c0c4b9 to your computer and use it in GitHub Desktop.

Select an option

Save musoftware/66502a6634f634b522c746a4c9c0c4b9 to your computer and use it in GitHub Desktop.
AverageValues Calc Best Result
public class AverageValues
{
public int intialValue { get; private set; }
public int currentValue = 0;
double oldResult = 0;
bool StopIncrease = false;
int incrementValue = 0;
public AverageValues(int intialValue = 100, int incrementValue = 10)
{
this.intialValue = intialValue;
currentValue = intialValue;
this.incrementValue = incrementValue;
}
public void CalcAverage(Func<int, int> action)
{
Stopwatch stopw = new Stopwatch();
stopw.Start();
var result = action.Invoke(currentValue);
stopw.Stop();
double calcResult = (double)result / stopw.Elapsed.TotalSeconds;
if (oldResult < calcResult)
{
if (StopIncrease) return;
currentValue += 10;
oldResult = calcResult;
}
else
{
currentValue -= 10;
StopIncrease = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment