using System; using System.Diagnostics; using System.Linq; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.EvolutionTracking.Tests { [TestClass] public class QueryMatchPerformanceTest { private const int MaxDepth = 12; private const int MaxLength = 50; private const int N = 50000; private const int Repetitions = 5; [TestMethod] public void TestQueryMatchPerformance() { var grammar = new TypeCoherentExpressionGrammar(); grammar.ConfigureAsDefaultRegressionGrammar(); var creator = new ProbabilisticTreeCreator(); var random = new MersenneTwister(); var trees = Enumerable.Range(0, N).Select(x => creator.CreateTree(random, grammar, MaxLength, MaxDepth)).ToList(); var comparer = new SymbolicExpressionTreeNodeEqualityComparer { MatchConstantValues = false, MatchVariableWeights = false, MatchVariableNames = true }; var qm = new QueryMatch(comparer) { MatchParents = true }; // warmup int count = trees.Skip(1).Count(x => qm.Match(x, trees[0])); var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < Repetitions; ++i) count = trees.Skip(1).Count(x => x.Length >= trees[0].Length && qm.Match(x, trees[0])); sw.Stop(); Console.WriteLine("Count: " + count); Console.WriteLine("Query matches per second: " + (N - 1) / (sw.ElapsedMilliseconds / 1000.0) * Repetitions); Console.WriteLine("Total time: " + sw.ElapsedMilliseconds / 1000.0 + "s"); sw.Restart(); for (int i = 0; i < Repetitions; ++i) count = qm.GetMatchingTrees(trees.Skip(1), trees[0]).Count(); sw.Stop(); Console.WriteLine("Count: " + count); Console.WriteLine("Optimized query matches per second: " + (N - 1) / (sw.ElapsedMilliseconds / 1000.0) * Repetitions); Console.WriteLine("Total time: " + sw.ElapsedMilliseconds / 1000.0 + "s"); } } }