Let be the following binary search function:
public static bool BinarySearching(int[] array, int searchValue)
{
Array.Sort(array);
double p = 0;
double q = 0;
int r = array.Length - 1;
while (p <= r)
{
q = Math.Floor((p + r) / 2);
if (array[(int)q] == searchValue)
{
return true;
}
else if (array[(int)q] != searchValue && array[(int)q] > searchValue)
{
r = (int)(q - 1);
}
else if (array[(int)q] != searchValue && array[(int)q] < searchValue)
{
p = (int)(q + 1);
}
}
return false;
}
}
If we want to measure its execution time, we would do something like
var watch = System.Diagnostics.Stopwatch.StartNew();
BinarySearching(int[] array, int searchValue);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
But is it any more bautiful way to measure like through the separate function, such that varaible is calculated function itself? For example, in pseudocode it is
public static string ComplexityCounter(bool algorithm(int[] array, int searchValue))
{
var watch = System.Diagnostics.Stopwatch.StartNew();
algorithm(int[] array, int searchValue);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
string result = elapsedMs.ToString();
return result;
}
And, sure it doesn't work in terms of C#, can you help me to revise it or to propose your own try ? The most interesting is to find such structure for all algorithms regardless of the type of variable it produces.