Skip to content

Instantly share code, notes, and snippets.

@leetmikeal
Created December 21, 2016 05:26
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/69d6d18caed6ba52a4d478a5c01a3c38 to your computer and use it in GitHub Desktop.
Save leetmikeal/69d6d18caed6ba52a4d478a5c01a3c38 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ListPerformanceTest003
{
class Program
{
static void Main(string[] args)
{
// 3D粒子数
int numStart = 10;
int numEnd = 10000;
int numStep = 10;
// 繰り返し回数
int loop = 10;
Random rand = new Random(1234567);
JobItem job1 = new JobItem()
{
Message = "List<class>",
Action = (sw, num) =>
{
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(),
});
Thread.Sleep(10);
sw.Start();
List<CoordinateClass> copiedList = new List<CoordinateClass>();
foreach (var item in list1)
copiedList.Add(item.Clone());
sw.Stop();
},
};
JobItem job2 = new JobItem()
{
Message = "List<struct>",
Action = (sw, num) =>
{
List<CoordinateStruct> list1 = new List<CoordinateStruct>();
for (int i = 0; i < num; i++)
list1.Add(new CoordinateStruct()
{
X = rand.NextDouble(),
Y = rand.NextDouble(),
Z = rand.NextDouble(),
});
Thread.Sleep(10);
sw.Start();
List<CoordinateStruct> copiedList = new List<CoordinateStruct>(list1);
sw.Stop();
},
};
List<JobItem> jobList = new List<JobItem>()
{
job1,
job2,
};
foreach (var job in jobList)
{
for (int num = numStart; num < numEnd; num += numStep)
{
Test(job, num, loop);
}
WriteLog(""); // 空白行挿入
}
}
public static void Test(JobItem jobItem, int num, int loop)
{
Action<Stopwatch, int> action = jobItem.Action;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < loop; i++)
{
action(sw, num);
System.Threading.Thread.Sleep(10);
}
string msg = string.Format("{0, -20} : num({1}) : {2:F3} msec.", jobItem.Message, num, (double)sw.ElapsedTicks / ((double)Stopwatch.Frequency * loop) * 1000);
WriteLog(msg);
}
public static void WriteLog(string msg)
{
Console.WriteLine(msg);
// ファイル出力
StreamWriter sw = new StreamWriter(SaveFile, true, Encoding.UTF8);
sw.WriteLine(msg);
sw.Close();
}
static string SaveFile = @"C:\Users\tamaki\Desktop\log.txt";
}
public class JobItem
{
public string Message { get; set; }
public Action<Stopwatch, int> Action { get; set; }
}
public class CoordinateClass
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public List<int> List { get; set; }
public CoordinateClass Clone()
{
return new CoordinateClass()
{
X = this.X,
Y = this.Y,
Z = this.Z,
};
}
}
public struct CoordinateStruct
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public List<int> List { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment