Advertisement
Guest User

C# array contest

a guest
Sep 27th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace ArrayTest {
  5.  
  6.     class Program {
  7.         const int SizeX = 512;
  8.         const int SizeY = 512;
  9.         const int SizeZ = 128;
  10.         const int Repets = 20;
  11.  
  12.         static void Main(string[] args) {
  13.            
  14.             //массивы массивов
  15.             var allocateJagged = TimeTest.Calc("Test allocate jagged array", _ => {
  16.                 var arrX = new int[SizeX][][];
  17.                 for (var x = 0; x < SizeX; x++) {
  18.                     var arrY = new int[SizeY][];
  19.                     for (var y = 0; y < SizeY; y++)
  20.                         arrY[y] = new int[SizeZ];
  21.                     arrX[x] = arrY;
  22.                 }
  23.                 return arrX;
  24.             });
  25.             var jaggedArr = allocateJagged.Data;
  26.             var xyzRagged = TimeTest.Exec(Repets, "Test fill jagged array in xyz order", _ => {
  27.                 var i = 0;
  28.                 for (var x = 0; x < SizeX; x++)
  29.                     for (var y = 0; y < SizeY; y++)
  30.                         for (var z = 0; z < SizeZ; z++)
  31.                             jaggedArr[x][y][z] = i++;
  32.             });
  33.  
  34.             //многомерные массивы
  35.             var allocateMultdim = TimeTest.Calc("Test allocate multidimension array", _ => {
  36.                 return new int[SizeX, SizeY, SizeZ];
  37.             });
  38.             var multdimArr = allocateMultdim.Data;
  39.             var xyzMultdim = TimeTest.Exec(Repets, "Test fill multidimension in xyz order", _ => {
  40.                 var i = 0;
  41.                 for (var x = 0; x < SizeX; x++)
  42.                     for (var y = 0; y < SizeY; y++)
  43.                         for (var z = 0; z < SizeZ; z++)
  44.                             multdimArr[x, y, z] = i++;
  45.             });
  46.             var xyzMultdimUnfase = TimeTest.Exec(Repets, "Test unsafe fill multidimension in xyz order", _ => {
  47.                 unsafe {
  48.                     var i = 0;
  49.                     fixed (int* prt = multdimArr) {
  50.                         for (var x = 0; x < SizeX; x++)
  51.                             for (var y = 0; y < SizeY; y++)
  52.                                 for (var z = 0; z < SizeZ; z++)
  53.                                     prt[(x * SizeY + y) * SizeZ + z] = i++;
  54.                     }
  55.                 }
  56.             });
  57.  
  58.             //плоские массивы
  59.             var allocateFlat = TimeTest.Calc("Test allocate flat array", _ => {
  60.                 return new Flat3DArray(SizeX, SizeY, SizeZ);
  61.             });
  62.             var flatArr = allocateFlat.Data;
  63.             var xyzFlat = TimeTest.Exec(Repets, "Test fill flat array in xyz order", _ => {
  64.                 var i = 0;
  65.                 for (var x = 0; x < SizeX; x++)
  66.                     for (var y = 0; y < SizeY; y++)
  67.                         for (var z = 0; z < SizeZ; z++)
  68.                             flatArr.Set(x, y, z, i++);
  69.             });
  70.             var xyzFlatUnsafe = TimeTest.Exec(Repets, "Test unssafe fill flat array in xyz order", _ => {
  71.                 unsafe {
  72.                     var i = 0;
  73.                     fixed (int* prt = flatArr.FlatArray) {
  74.                         for (var x = 0; x < SizeX; x++)
  75.                             for (var y = 0; y < SizeY; y++)
  76.                                 for (var z = 0; z < SizeZ; z++)
  77.                                     prt[(x * SizeY + y) * SizeZ + z] = i++;
  78.                     }
  79.                 }
  80.             });
  81.  
  82.             Console.WriteLine(allocateJagged);
  83.             Console.WriteLine(xyzRagged);
  84.             Console.WriteLine(allocateMultdim);
  85.             Console.WriteLine(xyzMultdim);
  86.             Console.WriteLine(xyzMultdimUnfase);
  87.             Console.WriteLine(allocateFlat);
  88.             Console.WriteLine(xyzFlat);
  89.             Console.WriteLine(xyzFlatUnsafe);
  90.  
  91.             Console.ReadLine();
  92.         }
  93.  
  94.     }
  95.  
  96.  
  97.     public class Flat3DArray {
  98.         public readonly int[] FlatArray;
  99.         public readonly int SizeX;
  100.         public readonly int SizeY;
  101.         public readonly int SizeZ;
  102.         public Flat3DArray(int sizeX, int sizeY, int sizeZ) {
  103.             FlatArray = new int[sizeX * sizeY * sizeZ];
  104.             SizeX = sizeX;
  105.             SizeY = sizeY;
  106.             SizeZ = sizeZ;
  107.         }
  108.         public int Get(int x, int y, int z) {
  109.             return FlatArray[(x * SizeY + y) * SizeZ + z];
  110.         }
  111.         public void Set(int x, int y, int z, int value) {
  112.             FlatArray[(x * SizeY + y) * SizeZ + z] = value;
  113.         }
  114.     }
  115.  
  116.  
  117.     public class TimeTest {
  118.  
  119.         public class Info {
  120.             public readonly string Name;
  121.             public readonly long ElapsedMilliseconds;
  122.             public readonly int Repeats;
  123.             public Info(long elapsedMilliseconds, string name, int repeats = 1) {
  124.                 Repeats = repeats;
  125.                 ElapsedMilliseconds = elapsedMilliseconds;
  126.                 Name = name;
  127.             }
  128.             public override string ToString() {
  129.                 return Repeats == 1
  130.                     ? string.Format("{1}: {0}ms", ElapsedMilliseconds, Name)
  131.                     : string.Format("{2}: {0}ms ({3} repeats, avr {1}ms)",
  132.                                     ElapsedMilliseconds, ElapsedMilliseconds / Repeats, Name, Repeats);
  133.             }
  134.         }
  135.  
  136.         public class InfoData<T> : Info {
  137.             public readonly T Data;
  138.             public InfoData(T data, long elapsedMilliseconds, string name)
  139.                 : base(elapsedMilliseconds, name) {
  140.                 Data = data;
  141.             }
  142.         }
  143.  
  144.         public static InfoData<T> Calc<T>(string testName, Func<bool, T> func) {
  145.  
  146.             Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
  147.             var timer = Stopwatch.StartNew();
  148.             var v = func(true);
  149.             timer.Stop();
  150.             Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
  151.             return new InfoData<T>(v, timer.ElapsedMilliseconds, testName);
  152.         }
  153.  
  154.         public static Info Exec(int repeats, string testName, Action<bool> act) {
  155.             Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
  156.             var timer = Stopwatch.StartNew();
  157.             for (var i = 0; i < repeats; i++)
  158.                 act(true);
  159.             timer.Stop();
  160.             Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
  161.             return new Info(timer.ElapsedMilliseconds, testName, repeats);
  162.         }
  163.  
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement