편집 : 내 대답이 정확하게 질문에 대답하지 않았 음을 알았지 만 실제 단점은 없지만 타이밍 결과에서 실제 단점도 없습니다. 인라인 속성 게터의 차이는 5 억 회 반복에서 0.002 초입니다. 내 테스트 케이스는 구조체를 사용하기 때문에 100 % 정확하지 않을 수 있습니다. 왜냐하면 지터에 약간의 경고가 있고 구조체로 인라인되기 때문입니다.
항상 그렇듯이 실제로 아는 유일한 방법은 테스트를 작성하고 알아내는 것입니다. 다음 구성에 대한 결과는 다음과 같습니다.
Windows 7 Home
8GB ram
64bit os
i5-2300 2.8ghz
다음 설정으로 프로젝트를 비 웁니다.
.NET 4.5
Release mode
Start without debugger attached - CRUCIAL
Unchecked "Prefer 32-bit" under project build settings
결과
struct get property : 0.3097832 seconds
struct inline get property : 0.3079076 seconds
struct method call with params : 1.0925033 seconds
struct inline method call with params : 1.0930666 seconds
struct method call without params : 1.5211852 seconds
struct intline method call without params : 1.2235001 seconds
이 코드로 테스트했습니다.
class Program
{
const int SAMPLES = 5;
const int ITERATIONS = 100000;
const int DATASIZE = 1000;
static Random random = new Random();
static Stopwatch timer = new Stopwatch();
static Dictionary<string, TimeSpan> timings = new Dictionary<string, TimeSpan>();
class SimpleTimer : IDisposable
{
private string name;
public SimpleTimer(string name)
{
this.name = name;
timer.Restart();
}
public void Dispose()
{
timer.Stop();
TimeSpan ts = TimeSpan.Zero;
if (timings.ContainsKey(name))
ts = timings[name];
ts += timer.Elapsed;
timings[name] = ts;
}
}
[StructLayout(LayoutKind.Sequential, Size = 4)]
struct TestStruct
{
private int x;
public int X { get { return x; } set { x = value; } }
}
[StructLayout(LayoutKind.Sequential, Size = 4)]
struct TestStruct2
{
private int x;
public int X
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return x; }
set { x = value; }
}
}
[StructLayout(LayoutKind.Sequential, Size = 8)]
struct TestStruct3
{
private int x;
private int y;
public void Update(int _x, int _y)
{
x += _x;
y += _y;
}
}
[StructLayout(LayoutKind.Sequential, Size = 8)]
struct TestStruct4
{
private int x;
private int y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(int _x, int _y)
{
x += _x;
y += _y;
}
}
[StructLayout(LayoutKind.Sequential, Size = 8)]
struct TestStruct5
{
private int x;
private int y;
public void Update()
{
x *= x;
y *= y;
}
}
[StructLayout(LayoutKind.Sequential, Size = 8)]
struct TestStruct6
{
private int x;
private int y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update()
{
x *= x;
y *= y;
}
}
static void RunTests()
{
for (var i = 0; i < SAMPLES; ++i)
{
Console.Write("Sample {0} ... ", i);
RunTest1();
RunTest2();
RunTest3();
RunTest4();
RunTest5();
RunTest6();
Console.WriteLine(" complate");
}
}
static int RunTest1()
{
var data = new TestStruct[DATASIZE];
var temp = 0;
unchecked
{
//init the data, just so jitter can't make assumptions
for (var j = 0; j < DATASIZE; ++j)
data[j].X = random.Next();
using (new SimpleTimer("struct get property"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
temp += data[j].X;
}
}
}
}
//again need variables to cross scopes to make sure the jitter doesn't do crazy optimizations
return temp;
}
static int RunTest2()
{
var data = new TestStruct2[DATASIZE];
var temp = 0;
unchecked
{
//init the data, just so jitter can't make assumptions
for (var j = 0; j < DATASIZE; ++j)
data[j].X = random.Next();
using (new SimpleTimer("struct inline get property"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
temp += data[j].X;
}
}
}
}
//again need variables to cross scopes to make sure the jitter doesn't do crazy optimizations
return temp;
}
static void RunTest3()
{
var data = new TestStruct3[DATASIZE];
unchecked
{
using (new SimpleTimer("struct method call with params"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
data[j].Update(j, i);
}
}
}
}
}
static void RunTest4()
{
var data = new TestStruct4[DATASIZE];
unchecked
{
using (new SimpleTimer("struct inline method call with params"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
data[j].Update(j, i);
}
}
}
}
}
static void RunTest5()
{
var data = new TestStruct5[DATASIZE];
unchecked
{
using (new SimpleTimer("struct method call without params"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
data[j].Update();
}
}
}
}
}
static void RunTest6()
{
var data = new TestStruct6[DATASIZE];
unchecked
{
using (new SimpleTimer("struct intline method call without params"))
{
for (var j = 0; j < DATASIZE; ++j)
{
for (var i = 0; i < ITERATIONS; ++i)
{
//use some math to make sure its not optimized out (aka don't use an incrementor)
data[j].Update();
}
}
}
}
}
static void Main(string[] args)
{
RunTests();
DumpResults();
Console.Read();
}
static void DumpResults()
{
foreach (var kvp in timings)
{
Console.WriteLine("{0,-50}: {1} seconds", kvp.Key, kvp.Value.TotalSeconds);
}
}
}