Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. namespace Lab2 {
  7.     public static class SimplePerfomanceEvaluator {
  8.         public static IDictionary<string, int> EvaluateArrayPerfomance<TItem>(int nRows, int nColumns, Action<TItem> testAction) where TItem : class, new() {
  9.             var result = new Dictionary<string, int>();
  10.  
  11.             //TwoDimensionalArray
  12.  
  13.             var twoDimensionalArray = new TItem[nRows, nColumns];
  14.             for (int rowIndex = 0; rowIndex < twoDimensionalArray.GetLength(0); rowIndex++) {
  15.                 for (int columnIndex = 0; columnIndex < twoDimensionalArray.GetLength(1); columnIndex++) {
  16.                     twoDimensionalArray[rowIndex, columnIndex] = new TItem();
  17.                 }
  18.             }
  19.  
  20.             var startTime = Environment.TickCount;
  21.  
  22.             for (int rowIndex = 0; rowIndex < twoDimensionalArray.GetLength(0); rowIndex++) {
  23.                 for (int columnIndex = 0; columnIndex < twoDimensionalArray.GetLength(1); columnIndex++) {
  24.                     testAction(twoDimensionalArray[rowIndex, columnIndex]);
  25.                 }
  26.             }
  27.             var endTime = Environment.TickCount;
  28.             result.Add("Two dimensional array", endTime - startTime);
  29.  
  30.             //OneDimensionalArray
  31.             int size = nRows * nColumns;
  32.             var oneDimensionalArray = new TItem[size];
  33.  
  34.             for (var index = 0; index < oneDimensionalArray.GetLength(0); index++) {
  35.                 oneDimensionalArray[index] = new TItem();
  36.             }
  37.  
  38.             startTime = Environment.TickCount;
  39.  
  40.             for (var index = 0; index < oneDimensionalArray.GetLength(0); index++) {
  41.                 testAction(oneDimensionalArray[index]);
  42.             }
  43.  
  44.             endTime = Environment.TickCount;
  45.             result.Add("One dimensional array", endTime - startTime);
  46.  
  47.  
  48.             //JaggedArray;
  49.  
  50.             var rand = new Random();
  51.             var maxSize = nRows * nColumns;
  52.             int koef = (int)Math.Sqrt(maxSize); //Magic
  53.             var rowsCount = rand.Next(1, maxSize / koef);
  54.             var jaggedArray = new TItem[rowsCount][];
  55.             var residual = maxSize - rowsCount;
  56.             int curentSize = 0;
  57.  
  58.             for (int i = 0; i < rowsCount; i++) {
  59.                 var columnsCount = rand.Next(1, (residual / (koef / 2)) + 1);
  60.  
  61.                 if (i == rowsCount - 1 && curentSize != maxSize) {
  62.                     columnsCount = Math.Abs(curentSize - maxSize);
  63.                 }
  64.                 curentSize += columnsCount;
  65.                 jaggedArray[i] = new TItem[columnsCount];
  66.                 for (var j = 0; j < columnsCount; j++) {
  67.                     jaggedArray[i][j] = new TItem();
  68.                 }
  69.             }
  70.  
  71.             startTime = Environment.TickCount;
  72.             int p = 0;
  73.             for (int rowIndex = 0; rowIndex < jaggedArray.GetLength(0); rowIndex++) {
  74.                 Console.WriteLine(jaggedArray[rowIndex].Length);
  75.                 p += jaggedArray[rowIndex].Length;
  76.                 for (int columnIndex = 0; columnIndex < jaggedArray[rowIndex].Length; columnIndex++) {
  77.                     testAction(jaggedArray[rowIndex][columnIndex]);
  78.                 }
  79.             }
  80.             endTime = Environment.TickCount;
  81.  
  82.             result.Add("Jagged array", endTime - startTime);
  83.  
  84.             return result;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement