public static void ComparePerformanceOfCachedAndOnTheFlySegmentStateCalculators( string logFilePath, int repetitions, BitMatrix board, Matrix <Cell> cellMatrix, Matrix <SegmentState> vertSegStateMatrix, Matrix <SegmentState> horizSegStateMatrix, Matrix <bool> boolMatrix) { // Cache based segment state calculator: CacheBasedSegmentStateCalculator cacheSegStateCalculator = new CacheBasedSegmentStateCalculator(horizSegStateMatrix, vertSegStateMatrix); PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using cache calculator", repetitions, Tuple.Create(board.BottomRight, cacheSegStateCalculator), CalculateSegmentStatesUsingCache); // Calculation based segment state calculator: CalculationBasedSegmentStateCalculator calcSegStateCalculator = new CalculationBasedSegmentStateCalculator(board); PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using direct BitMatrix-based calculator", repetitions, Tuple.Create(board.BottomRight, calcSegStateCalculator), CalculateSegmentStatesUsingCalculationBasedCalculator); // Bool Matrix based on the fly segment state calculator: OnTheFlyBoolMatrixBasedSegmentStateCalculator boolMatrixBasedSegStateCalculator = new OnTheFlyBoolMatrixBasedSegmentStateCalculator(boolMatrix); PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using direct bool matrix based calculator", repetitions, Tuple.Create(boolMatrix.BottomRight, boolMatrixBasedSegStateCalculator), CalculateSegmentStatesUsingOnTheFlyBoolBoolBasedCalculator); CellMatrixBasedSegmentStateCalculator cellCacheSegStateCalculator = new CellMatrixBasedSegmentStateCalculator(board, cellMatrix); PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using cell matrix-based cache calculator", repetitions, Tuple.Create(board.BottomRight, cellCacheSegStateCalculator), CalculateSegmentStatesUsingCellCacheBasedCalculator); /* Compare segment state calculation times over random points for cached and calculated segment state calculators, * to make sure the cache calculator is not just benefiting from memory cache effects due to the sequence of points: */ Core.Point[] randomPoints = DataGenerator.GenerateRandomPoints(board.TopLeft, board.BottomRight, 10000); // Cache based segment state calculator: PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using cache calculator", repetitions, Tuple.Create(randomPoints, cacheSegStateCalculator), CalculateSegmentStatesOnRandomPointsUsingCache); PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using cell matrix-based cache calculator", repetitions, Tuple.Create(randomPoints, cellCacheSegStateCalculator), CalculateSegmentStatesOnRandomPointsUsingCache); // Calculation based segment state calculator: PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using on the fly calculator", repetitions, Tuple.Create(randomPoints, calcSegStateCalculator), CalculateSegmentStatesOnRandomPointsUsingCalculationBasedCalculator); // Bool Matrix based on the fly segment state calculator: PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using on the fly bool matrix based calculator", repetitions, Tuple.Create(randomPoints, boolMatrixBasedSegStateCalculator), CalculateSegmentStatesOnRandomPointsUsingOnTheFlyBoolBoolBasedCalculator); }
public static void CalculateSegmentStatesOnRandomPointsUsingCache(Tuple <Core.Point[], CacheBasedSegmentStateCalculator> tuple) { Core.Point[] randomPoints = tuple.Item1; CacheBasedSegmentStateCalculator calculator = tuple.Item2; foreach (Axis axis in BoardHelper.AllRealAxes) { foreach (Core.Point randomPoint in randomPoints) { SegmentState segState = calculator.GetSegmentState(axis, randomPoint); } } }
private static void CalculateSegmentStatesUsingCache(Tuple <Core.Point, CacheBasedSegmentStateCalculator> tuple) { AndrewTweddle.BattleCity.Core.Point bottomRight = tuple.Item1; CacheBasedSegmentStateCalculator calculator = tuple.Item2; foreach (Axis axis in BoardHelper.AllRealAxes) { for (int x = 0; x < bottomRight.X; x++) { for (int y = 0; y < bottomRight.Y; y++) { SegmentState segState = calculator.GetSegmentState(axis, x, y); } } } }
public void TestDistanceCalculator(string logFilePath, int repetitions, int smallRepetitions) { SegStateCalculator = new CacheBasedSegmentStateCalculator(HorizSegmentStateMatrix, VertSegmentStateMatrix); TankEdgeMatrix = PerformanceTestHelper.TimeFunction( logFilePath, "Calculate tank edge matrix using TankEdgeCalculator", repetitions, CalculateTankOuterEdgeMatrix); MobileState tankState1 = new MobileState(new Point(20, 6), Direction.UP, isActive: true); DistancesFromTank1 = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath, "Test Distance Calculator for tank 1", smallRepetitions, tankState1, CalculateDistancesForTank); MobileState tankState2 = new MobileState(new Point(56, 6), Direction.UP, isActive: true); DistancesFromTank2 = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath, "Test Distance Calculator for tank 2", smallRepetitions, tankState2, CalculateDistancesForTank); }
public static Matrix <SegmentState[]> CalculateTankInnerEdges( CacheBasedSegmentStateCalculator segStateCalculator, BitMatrix board) { int segX; int segY; Axis axisOfMovement; SegmentState segState; Direction dir; Matrix <SegmentState[]> tankEdgeMatrix = new Matrix <SegmentState[]>(board.TopLeft, board.Width, board.Height); int topLeftX = board.TopLeft.X; int topLeftY = board.TopLeft.Y; int bottomRightX = board.BottomRight.X; int bottomRightY = board.BottomRight.Y; for (int x = topLeftX; x <= bottomRightX; x++) { for (int y = topLeftY; y <= bottomRightY; y++) { #if CONDITIONAL_BREAKPOINT_TankEdgeCalculator_CalculateTankInnerEdges Debug.Assert(x != 28 || y != 72, "Conditional breakpoint"); #endif SegmentState[] segStatesByDir = new SegmentState[Constants.EDGE_COUNT]; tankEdgeMatrix[x, y] = segStatesByDir; // 1. Vertical edges: axisOfMovement = Axis.Vertical; segX = x; // 1.1 Top edge of tank: dir = Direction.UP; segY = y - Constants.TANK_EXTENT_OFFSET; if (segY >= topLeftY) { segState = segStateCalculator.GetSegmentState(axisOfMovement, segX, segY); segStatesByDir[(byte)dir] = segState; } // 1.2 Bottom edge of tank: dir = Direction.DOWN; segY = y + Constants.TANK_EXTENT_OFFSET; if (segY <= bottomRightY) { segState = segStateCalculator.GetSegmentState(axisOfMovement, segX, segY); segStatesByDir[(byte)dir] = segState; } // 2. Horizontal edges: axisOfMovement = Axis.Horizontal; segY = y; // 2.1 Left edge of tank: dir = Direction.LEFT; segX = x - Constants.TANK_EXTENT_OFFSET; if (segX >= topLeftX) { segState = segStateCalculator.GetSegmentState(axisOfMovement, segX, segY); segStatesByDir[(byte)dir] = segState; } // 2.2 Right edge of tank: dir = Direction.RIGHT; segX = x + Constants.TANK_EXTENT_OFFSET; if (segX <= bottomRightX) { segState = segStateCalculator.GetSegmentState(axisOfMovement, segX, segY); segStatesByDir[(byte)dir] = segState; } } } return(tankEdgeMatrix); }