Reflection Time: 1692.1692ms
Lookup Time: 19.0019ms
Press Enter to exit
:나는이 오래된 질문이다 알지만, 내가 잘 수행 할 것으로 보인다 간단한 솔루션을 던질 거라고 생각하고 유지 상징
static void Main(string[] args)
{
int loopCount = 1000000; // 1,000,000 (one million) iterations
var timer = new Timer();
timer.Restart();
for (int i = 0; i < loopCount; i++)
Log(MethodBase.GetCurrentMethod(), "whee");
TimeSpan reflectionRunTime = timer.CalculateTime();
timer.Restart();
for (int i = 0; i < loopCount; i++)
Log((Action<string[]>)Main, "whee");
TimeSpan lookupRunTime = timer.CalculateTime();
Console.WriteLine("Reflection Time: {0}ms", reflectionRunTime.TotalMilliseconds);
Console.WriteLine(" Lookup Time: {0}ms", lookupRunTime.TotalMilliseconds);
Console.WriteLine();
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
}
public static void Log(Delegate info, string message)
{
// do stuff
}
public static void Log(MethodBase info, string message)
{
// do stuff
}
public class Timer
{
private DateTime _startTime;
public void Restart()
{
_startTime = DateTime.Now;
}
public TimeSpan CalculateTime()
{
return DateTime.Now.Subtract(_startTime);
}
}
이 코드를 실행하면 다음과 같은 결과가 날 수 있습니다
백만 회 반복의 경우 특히 은입니다. 특히 직선 반향과 비교하면 좋습니다. 메서드 그룹이 대리자 형식으로 캐스트되고 있으면 로깅에 대한 모든 방법으로 심볼 링크가 유지됩니다. 구피 마술 끈이 없어.
@loannis와 비슷한 질문이 여기에 있습니다. 링크를 확인하십시오. http://stackoverflow.com/questions/1466740/using-getcurrentmethod-in-supposedly-high-performance-code – RameshVel
daniels에서 "this.GetType()은 호출 당 2.5 ns가 필요하고 MethodBase.GetCurrentMethod()는 필요합니다. .DeclaringType은 호출 당 2490 ns를 필요로하므로 약 1200 배의 속도가 향상됩니다. " – RameshVel
@Ramesh : 그 질문에 이미 링크되어있는 것과 정확히 동일한 질문이 있음을 알았습니까? –