Skip to content

Instantly share code, notes, and snippets.

@leetmikeal
Created December 21, 2016 05:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leetmikeal/7c3fd61e5affeb0cd7e64ba7f9a13757 to your computer and use it in GitHub Desktop.
Save leetmikeal/7c3fd61e5affeb0cd7e64ba7f9a13757 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 ListPerformanceTest003
{
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(),
});
CoordinateClass[] list2 = new CoordinateClass[num];
for (int i = 0; i < num; i++)
list2[i] = new CoordinateClass()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
};
JobItem job1 = new JobItem()
{
Message = "List to List",
Action = () =>
{
List<CoordinateClass> copiedList = list1.ToList();
},
};
JobItem job2 = new JobItem()
{
Message = "List to Array",
Action = () =>
{
CoordinateClass[] copiedArray = list1.ToArray();
},
};
JobItem job3 = new JobItem()
{
Message = "Array to List",
Action = () =>
{
List<CoordinateClass> copiedList = list2.ToList();
},
};
JobItem job4 = new JobItem()
{
Message = "Array to Array",
Action = () =>
{
CoordinateClass[] copiedArray = list2.ToArray();
},
};
List<JobItem> jobList = new List<JobItem>()
{
job1, // 1番最初は遅延するので敢えて挿入
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