Advertisement
Guest User

C# array contest

a guest
Sep 26th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ArrTest2 {
  7.     class Program {
  8.  
  9.         static void Main(string[] args) {
  10.             const int sizeX = 512;
  11.             const int sizeY = 512;
  12.             const int sizeZ = 128;
  13.  
  14.             //массивы массивов
  15.             var allocateRagged = Calc("Test allocate ragged 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 raggedArr = allocateRagged.Data;
  26.             var xyzRagged = Exec(20, "Test fill ragged 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.                             raggedArr[x][y][z] = i++;
  32.             });
  33.  
  34.  
  35.             //многомерные массивы
  36.             var allocateMultdim = Calc("Test allocate multidimension array", _ => {
  37.                 return new int[sizeX, sizeY, sizeZ];
  38.             });
  39.             var multdimArr = allocateMultdim.Data;
  40.             var xyzMultdim = Exec(20, "Test fill multidimension in xyz order", _ => {
  41.                 var i = 0;
  42.                 for (var x = 0; x < sizeX; x++)
  43.                     for (var y = 0; y < sizeY; y++)
  44.                         for (var z = 0; z < sizeZ; z++)
  45.                             multdimArr[x, y, z] = i++;
  46.             });
  47.            
  48.            
  49.             //плоское массив
  50.             var allocateFlat = Calc("Test allocate flat array", _ => {
  51.                 return new Flat3dArray(sizeX, sizeY, sizeZ);
  52.             });
  53.             var flatArr = allocateFlat.Data;
  54.             var xyzFlat = Exec(20, "Test fill flat array in xyz order", _ => {
  55.                 var i = 0;
  56.                 for (var x = 0; x < sizeX; x++)
  57.                     for (var y = 0; y < sizeY; y++)
  58.                         for (var z = 0; z < sizeZ; z++)
  59.                             flatArr.Set(x, y, z, i++);
  60.             });
  61.             var xzyFlat = Exec(20, "Test fill flat array in xzy order", _ => {
  62.                 var i = 0;
  63.                 for (var x = 0; x < sizeX; x++)
  64.                     for (var z = 0; z < sizeZ; z++)
  65.                         for (var y = 0; y < sizeY; y++)
  66.                             flatArr.Set(x, y, z, i++);
  67.             });
  68.  
  69.             Console.WriteLine(allocateRagged);
  70.             Console.WriteLine(xyzRagged);
  71.             Console.WriteLine(allocateMultdim);
  72.             Console.WriteLine(xyzMultdim);
  73.             Console.WriteLine(allocateFlat);
  74.             Console.WriteLine(xyzFlat);
  75.             Console.WriteLine(xzyFlat);
  76.  
  77.             Console.ReadLine();
  78.         }
  79.  
  80.         public static TimeTestData<T> Calc<T>(string testName, Func<bool, T> func) {
  81.             Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
  82.             var s = DateTime.Now;
  83.             var v = func(true);
  84.             var e = DateTime.Now;
  85.             Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
  86.             return new TimeTestData<T>(v, s, e, testName);
  87.         }
  88.  
  89.         public static TimeTest Exec(int repeats, string testName, Action<bool> act) {
  90.             Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
  91.             var s = DateTime.Now;
  92.             for (var i = 0; i < repeats; i++)
  93.                 act(true);
  94.             var e = DateTime.Now;
  95.             Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
  96.             return new TimeTest(s, e, testName, repeats);
  97.         }
  98.  
  99.     }
  100.  
  101.  
  102.     public class Flat3dArray {
  103.         private int[] _arr;
  104.         public readonly int SizeX;
  105.         public readonly int SizeY;
  106.         public readonly int SizeZ;
  107.         public Flat3dArray(int sizeX, int sizeY, int sizeZ) {
  108.             _arr = new int[sizeX * sizeY * sizeZ];
  109.             SizeX = sizeX;
  110.             SizeY = sizeY;
  111.             SizeZ = sizeZ;
  112.         }
  113.         private int toFlat(int x, int y, int z) {
  114.             return x * SizeY * SizeZ + y * SizeZ + z;
  115.         }
  116.         public int Get(int x, int y, int z) {
  117.             return _arr[toFlat(x, y, z)];
  118.         }
  119.         public void Set(int x, int y, int z, int value) {
  120.             _arr[toFlat(x, y, z)] = value;
  121.         }
  122.     }
  123.  
  124.     public class TimeTest {
  125.         public readonly string Name;
  126.         public readonly long Ticks;
  127.         public readonly int Repeats;
  128.         public TimeTest(DateTime start, DateTime end, string name, int repeats = 1) {
  129.             Ticks = end.Ticks - start.Ticks;
  130.             Repeats = repeats;
  131.             Name = name;
  132.         }
  133.  
  134.         public override string ToString() {
  135.             var total = Ticks / 1000.0 / 10000.0;
  136.             return Repeats == 1
  137.                 ? string.Format("{1}: {0} seconds", total, Name)
  138.                 : string.Format("{2}: {0} seconds (repeated {3} avr {1} seconds)",
  139.                                 total, total / Repeats, Name, Repeats);
  140.         }
  141.  
  142.     }
  143.  
  144.     public class TimeTestData<T> : TimeTest {
  145.         public readonly T Data;
  146.         public TimeTestData(T data, DateTime start, DateTime end, string name)
  147.             : base(start, end, name) {
  148.             Data = data;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement