Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ArrTest2 {
- class Program {
- static void Main(string[] args) {
- const int sizeX = 512;
- const int sizeY = 512;
- const int sizeZ = 128;
- //массивы массивов
- var allocateRagged = Calc("Test allocate ragged 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 raggedArr = allocateRagged.Data;
- var xyzRagged = Exec(20, "Test fill ragged 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++)
- raggedArr[x][y][z] = i++;
- });
- //многомерные массивы
- var allocateMultdim = Calc("Test allocate multidimension array", _ => {
- return new int[sizeX, sizeY, sizeZ];
- });
- var multdimArr = allocateMultdim.Data;
- var xyzMultdim = Exec(20, "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 allocateFlat = Calc("Test allocate flat array", _ => {
- return new Flat3dArray(sizeX, sizeY, sizeZ);
- });
- var flatArr = allocateFlat.Data;
- var xyzFlat = Exec(20, "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 xzyFlat = Exec(20, "Test fill flat array in xzy order", _ => {
- var i = 0;
- for (var x = 0; x < sizeX; x++)
- for (var z = 0; z < sizeZ; z++)
- for (var y = 0; y < sizeY; y++)
- flatArr.Set(x, y, z, i++);
- });
- Console.WriteLine(allocateRagged);
- Console.WriteLine(xyzRagged);
- Console.WriteLine(allocateMultdim);
- Console.WriteLine(xyzMultdim);
- Console.WriteLine(allocateFlat);
- Console.WriteLine(xyzFlat);
- Console.WriteLine(xzyFlat);
- Console.ReadLine();
- }
- public static TimeTestData<T> Calc<T>(string testName, Func<bool, T> func) {
- Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
- var s = DateTime.Now;
- var v = func(true);
- var e = DateTime.Now;
- Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
- return new TimeTestData<T>(v, s, e, testName);
- }
- public static TimeTest Exec(int repeats, string testName, Action<bool> act) {
- Console.WriteLine("{1}| Starting: {0}", testName, DateTime.Now);
- var s = DateTime.Now;
- for (var i = 0; i < repeats; i++)
- act(true);
- var e = DateTime.Now;
- Console.WriteLine("{1}| Completed: {0}", testName, DateTime.Now);
- return new TimeTest(s, e, testName, repeats);
- }
- }
- public class Flat3dArray {
- private int[] _arr;
- public readonly int SizeX;
- public readonly int SizeY;
- public readonly int SizeZ;
- public Flat3dArray(int sizeX, int sizeY, int sizeZ) {
- _arr = new int[sizeX * sizeY * sizeZ];
- SizeX = sizeX;
- SizeY = sizeY;
- SizeZ = sizeZ;
- }
- private int toFlat(int x, int y, int z) {
- return x * SizeY * SizeZ + y * SizeZ + z;
- }
- public int Get(int x, int y, int z) {
- return _arr[toFlat(x, y, z)];
- }
- public void Set(int x, int y, int z, int value) {
- _arr[toFlat(x, y, z)] = value;
- }
- }
- public class TimeTest {
- public readonly string Name;
- public readonly long Ticks;
- public readonly int Repeats;
- public TimeTest(DateTime start, DateTime end, string name, int repeats = 1) {
- Ticks = end.Ticks - start.Ticks;
- Repeats = repeats;
- Name = name;
- }
- public override string ToString() {
- var total = Ticks / 1000.0 / 10000.0;
- return Repeats == 1
- ? string.Format("{1}: {0} seconds", total, Name)
- : string.Format("{2}: {0} seconds (repeated {3} avr {1} seconds)",
- total, total / Repeats, Name, Repeats);
- }
- }
- public class TimeTestData<T> : TimeTest {
- public readonly T Data;
- public TimeTestData(T data, DateTime start, DateTime end, string name)
- : base(start, end, name) {
- Data = data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement