Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace ArrayTest {
- class Program {
- const int SizeX = 512;
- const int SizeY = 512;
- const int SizeZ = 128;
- const int Repets = 20;
- static void Main(string[] args) {
- //массивы массивов
- var allocateJagged = TimeTest.Calc("Test allocate jagged array", _ => {
- var arrX = new int[SizeX][][];
- for (var x = 0; x < SizeX; x++) {
- var arrY = new int[SizeY][];
- for (var y = 0; y < SizeY; y++)
- arrY[y] = new int[SizeZ];
- arrX[x] = arrY;
- }
- return arrX;
- });
- var jaggedArr = allocateJagged.Data;
- var xyzRagged = TimeTest.Exec(Repets, "Test fill jagged array in xyz order", _ => {
- var i = 0;
- for (var x = 0; x < SizeX; x++)
- for (var y = 0; y < SizeY; y++)
- for (var z = 0; z < SizeZ; z++)
- jaggedArr[x][y][z] = i++;
- });
- //многомерные массивы
- var allocateMultdim = TimeTest.Calc("Test allocate multidimension array", _ => {
- return new int[SizeX, SizeY, SizeZ];
- });
- var multdimArr = allocateMultdim.Data;
- var xyzMultdim = TimeTest.Exec(Repets, "Test fill multidimension in xyz order", _ => {
- var i = 0;
- for (var x = 0; x < SizeX; x++)
- for (var y = 0; y < SizeY; y++)
- for (var z = 0; z < SizeZ; z++)
- multdimArr[x, y, z] = i++;
- });
- var xyzMultdimUnfase = TimeTest.Exec(Repets, "Test unsafe fill multidimension in xyz order", _ => {
- unsafe {
- var i = 0;
- fixed (int* prt = multdimArr) {
- for (var x = 0; x < SizeX; x++)
- for (var y = 0; y < SizeY; y++)
- for (var z = 0; z < SizeZ; z++)
- prt[(x * SizeY + y) * SizeZ + z] = i++;
- }
- }
- });
- //плоские массивы
- var allocateFlat = TimeTest.Calc("Test allocate flat array", _ => {
- return new Flat3DArray(SizeX, SizeY, SizeZ);
- });
- var flatArr = allocateFlat.Data;
- var xyzFlat = TimeTest.Exec(Repets, "Test fill flat array in xyz order", _ => {
- var i = 0;
- for (var x = 0; x < SizeX; x++)
- for (var y = 0; y < SizeY; y++)
- for (var z = 0; z < SizeZ; z++)
- flatArr.Set(x, y, z, i++);
- });
- var xyzFlatUnsafe = TimeTest.Exec(Repets, "Test unssafe fill flat array in xyz order", _ => {
- unsafe {
- var i = 0;
- fixed (int* prt = flatArr.FlatArray) {
- for (var x = 0; x < SizeX; x++)
- for (var y = 0; y < SizeY; y++)
- for (var z = 0; z < SizeZ; z++)
- prt[(x * SizeY + y) * SizeZ + z] = i++;
- }
- }
- });
- Console.WriteLine(allocateJagged);
- Console.WriteLine(xyzRagged);
- Console.WriteLine(allocateMultdim);
- Console.WriteLine(xyzMultdim);
- Console.WriteLine(xyzMultdimUnfase);
- Console.WriteLine(allocateFlat);
- Console.WriteLine(xyzFlat);
- Console.WriteLine(xyzFlatUnsafe);
- Console.ReadLine();
- }
- }
- public class Flat3DArray {
- public readonly int[] FlatArray;
- public readonly int SizeX;
- public readonly int SizeY;
- public readonly int SizeZ;
- public Flat3DArray(int sizeX, int sizeY, int sizeZ) {
- FlatArray = new int[sizeX * sizeY * sizeZ];
- SizeX = sizeX;
- SizeY = sizeY;
- SizeZ = sizeZ;
- }
- public int Get(int x, int y, int z) {
- return FlatArray[(x * SizeY + y) * SizeZ + z];
- }
- public void Set(int x, int y, int z, int value) {
- FlatArray[(x * SizeY + y) * SizeZ + z] = value;
- }
- }
- public class TimeTest {
- public class Info {
- public readonly string Name;
- public readonly long ElapsedMilliseconds;
- public readonly int Repeats;
- public Info(long elapsedMilliseconds, string name, int repeats = 1) {
- Repeats = repeats;
- ElapsedMilliseconds = elapsedMilliseconds;
- Name = name;
- }
- public override string ToString() {
- return Repeats == 1
- ? string.Format("{1}: {0}ms", ElapsedMilliseconds, Name)
- : string.Format("{2}: {0}ms ({3} repeats, avr {1}ms)",
- ElapsedMilliseconds, ElapsedMilliseconds / Repeats, Name, Repeats);
- }
- }
- public class InfoData<T> : Info {
- public readonly T Data;
- public InfoData(T data, long elapsedMilliseconds, string name)
- : base(elapsedMilliseconds, name) {
- Data = data;
- }
- }
- public static InfoData<T> Calc<T>(string testName, Func<bool, T> func) {
- Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
- var timer = Stopwatch.StartNew();
- var v = func(true);
- timer.Stop();
- Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
- return new InfoData<T>(v, timer.ElapsedMilliseconds, testName);
- }
- public static Info Exec(int repeats, string testName, Action<bool> act) {
- Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
- var timer = Stopwatch.StartNew();
- for (var i = 0; i < repeats; i++)
- act(true);
- timer.Stop();
- Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
- return new Info(timer.ElapsedMilliseconds, testName, repeats);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement