Skip to content

Instantly share code, notes, and snippets.

@leetmikeal
Last active December 21, 2016 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leetmikeal/e4b1ab9bfff2af245a996bb00bed1f38 to your computer and use it in GitHub Desktop.
Save leetmikeal/e4b1ab9bfff2af245a996bb00bed1f38 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListPerformanceTest001
{
class Program
{
static void Main(string[] args)
{
// 3D粒子数
int num = 100000;
// 繰り返し回数
int loop = 100;
Random rand = new Random(1234567);
List<CoordinateClass> list1 = new List<CoordinateClass>();
for (int i = 0; i < num; i++)
list1.Add(new CoordinateClass()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
});
JobItem job1 = new JobItem()
{
Message = "List<class>",
Action = () =>
{
List<CoordinateClass> copiedList = new List<CoordinateClass>();
foreach (var item in list1)
copiedList.Add(item.Clone());
// エラー
//List<CoordinateClass> copiedList = new List<CoordinateClass>(list1.Count);
//for (int i = 0; i < list1.Count; i++)
// copiedList[i] = list1[i].Clone();
},
};
List<CoordinateStruct> list2 = new List<CoordinateStruct>();
for (int i = 0; i < num; i++)
list2.Add(new CoordinateStruct()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
});
JobItem job2 = new JobItem()
{
Message = "List<struct>",
Action = () =>
{
List<CoordinateStruct> copiedList = new List<CoordinateStruct>(list2);
},
};
CoordinateClass[] list3 = new CoordinateClass[num];
for (int i = 0; i < num; i++)
list3[i] = new CoordinateClass()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
};
JobItem job3 = new JobItem()
{
Message = "class[]",
Action = () =>
{
CoordinateClass[] copiedList = new CoordinateClass[list3.Length];
for (int i = 0; i < copiedList.Length; i++)
copiedList[i] = list3[i].Clone();
},
};
CoordinateStruct[] list4 = new CoordinateStruct[num];
for (int i = 0; i < num; i++)
list4[i] = new CoordinateStruct()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
};
JobItem job4 = new JobItem()
{
Message = "struct[]",
Action = () =>
{
CoordinateStruct[] copiedList = new CoordinateStruct[list4.Length];
list4.CopyTo(copiedList, 0);
},
};
List<JobItem> jobList = new List<JobItem>()
{
job1,
job2,
job3,
job4,
};
foreach (var job in jobList)
Test(job, loop);
}
public static void Test(JobItem jobItem, int loop)
{
Action action = jobItem.Action;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < loop; i++)
{
sw.Start();
action();
sw.Stop();
System.Threading.Thread.Sleep(10);
}
Console.WriteLine("{0, -20} : {1:F3} msec.", jobItem.Message, (double)sw.ElapsedTicks / ((double)Stopwatch.Frequency * loop) * 1000);
}
}
public class JobItem
{
public string Message { get; set; }
public Action Action { get; set; }
}
public class CoordinateClass
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public CoordinateClass()
{
}
public CoordinateClass(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public CoordinateClass Clone()
{
return new CoordinateClass(this.X, this.Y, this.Z);
}
}
public struct CoordinateStruct
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment