Answer the question
In order to leave comments, you need to log in
Why does Stopwatch work like this?
There is this code:
static void Main()
{
for (int i = 2; i <= 15; i++)
{
Stopwatch timerForRecursiveFactorial = Stopwatch.StartNew();
int RecursiveFactorial = ComputeFactorialRecursive(i);
timerForRecursiveFactorial.Stop();
long ticksSpentOnRecursiveFactorial = timerForRecursiveFactorial.ElapsedTicks;
Console.WriteLine("Факториал вычеслен рекурсивно. Результат = " + RecursiveFactorial + ". Затрачено " + ticksSpentOnRecursiveFactorial + " тиков");
Stopwatch timerForInterativeFactorial = Stopwatch.StartNew();
int IterativeFactorial = ComputeFactorialIterative(i);
timerForInterativeFactorial.Stop();
long ticksSpentOnIterativeFactorial = timerForInterativeFactorial.ElapsedTicks;
Console.WriteLine("Факториал вычеслен итеративно. Результат = " + IterativeFactorial + ". Затрачено " + ticksSpentOnIterativeFactorial + " тиков");
if (ticksSpentOnIterativeFactorial < ticksSpentOnRecursiveFactorial)
Console.WriteLine("Победил итеративный метод");
else
Console.WriteLine("Победил рекурсивный метод");
}
Console.ReadKey();
}
Факториал вычеслен рекурсивно. Результат = 2. Затрачено 471 тиков
Факториал вычеслен итеративно. Результат = 2. Затрачено 526 тиков
Победил рекурсивный метод
Факториал вычеслен рекурсивно. Результат = 6. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 6. Затрачено 0 тиков
Победил итеративный метод
Факториал вычеслен рекурсивно. Результат = 24. Затрачено 0 тиков
Факториал вычеслен итеративно. Результат = 24. Затрачено 1 тиков
Победил рекурсивный метод
Факториал вычеслен рекурсивно. Результат = 120. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 120. Затрачено 1 тиков
Победил рекурсивный метод
Факториал вычеслен рекурсивно. Результат = 720. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 720. Затрачено 1 тиков
Победил рекурсивный метод
Факториал вычеслен рекурсивно. Результат = 5040. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 5040. Затрачено 0 тиков
Победил итеративный метод
Факториал вычеслен рекурсивно. Результат = 40320. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 40320. Затрачено 1 тиков
Победил рекурсивный метод
Факториал вычеслен рекурсивно. Результат = 362880. Затрачено 1 тиков
Факториал вычеслен итеративно. Результат = 362880. Затрачено 0 тиков
Победил итеративный метод
Answer the question
In order to leave comments, you need to log in
This is because of the JIT compiler that is used in .NET
MSIL bytecode is translated into machine code once by the JIT compiler and subsequent execution of that code is much faster because the target code has been generated and cached
Are you kidding me to time the calculation like that?) You don’t think on Electronics that several iterations / recursive calls will be executed so quickly that you won’t even notice. Mikhail probably named the probable reason for this behavior, but this does not negate the fact that you are doing the experiment poorly.
When measuring performance, the same calculations need to be performed many times, for example, 100 thousand times for each option, or even a million. Then you will get a more or less intelligible result for comparison, and you will not stumble upon all sorts of subtleties of the CLR.
Everything is correct. ElapsedTicks shows the number of ticks of the timer, but the timer does not have time to tick even once during the calculation of the factorial, or one tick happens accidentally. In the first iteration, many hundreds of ticks pass, since the JIT compiler works on the first call, the running time of the compiled code is negligible compared to the compilation time.
What to do:
Quite strange, the code looks correct, as a "crutch" I can advise you to reset both stopwatch periodically
And you put a break at the beginning of the loop and look at the state of the objects at each iteration. Also check that all code is executed on each iteration.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question