/// <summary> /// Evaluates the performance of the task. /// </summary> /// <param name="name">The name of the task.</param> /// <param name="task">The task to evaluate.</param> /// <param name="iterations">The number of iterations to evaluate the task.</param> /// <returns>The return value is the result of the evaluation.</returns> public EvaluationResult EvaluateTask(string name, Action task, uint iterations) { // Check the process is optimized if (!EnableUnoptimizedEvaluations && !DebuggingHelper.IsOptimizedProcess) { throw new Exception("Cannot perform benchmark tests because the process is not running under an optimized state. " + "Do not attach a debugger and compile the program under the Release mode in order to get the best performance. " + "To remove this exception, set the property EnableUnoptimizedEvaluations to true."); } // Execute the task the first time to jit the function task(); // Create the return value var evaluation = new EvaluationResult(name, MustStoreIterations); // Perform the evaluation according the number of iterations for (var i = 0; i < iterations; i++) { // Check if the memory must be cleaned up if (i % CleanUpInterval == 0) { // Clean the memory MemoryOptimizerHandler.OptimizeMemory(); } // Perform the evaluation var iteration = InternalEvaluateTask(task); // Add the iteration to the evaluation result evaluation.AddIteration(iteration); // Raise the iteration event OnIterationCompleted(iteration, evaluation); } // Raise the evaluation event OnEvaluationCompleted(evaluation); // Return the object return(evaluation); }