對於我的生活,我無法弄清楚我的代碼中的這種性能。我有,我測量它需要多長時間運行構造的容器對象(對象下方),在公共構造計時碼現在C#創建對象比構造函數調用慢得多
public class WorkUnit : IWorkUnit
{
private JobInformation m_JobInfo;
private MetaInfo m_MetaInfo;
private IPreProcJobInfo m_PreprocDetails;
readonly private Guid m_ID;
private Guid m_ParentID;
private Guid m_MasterJobID;
private string m_ErrorLog = string.Empty;
private PriorityKeeper m_Priority;
private WorkUnitClassification m_Classification;
private IJobPayload m_CachedPayload;
private IJobLogger m_Logger;
private EventHandler<JobEventArgs> m_IsFinished;
private ReaderWriterLockSlim m_Lock;
public WorkUnit(string InputXML, Guid JobID, IJobLogger Logger)
{
DateTime overstarttime = DateTime.Now;
try
{
....Do Stuff....
}
catch(XMLException e)
{...}
catch(Exception e)
{
...
throw;
}
double time = (DateTime.Now - overstarttime).TotalMilliseconds
Console.WriteLine("{0}", time);
}
/// <summary>
/// Private Constructor used to create children
/// </summary>
private WorkUnit(Guid MasterID, Guid ParentID, WorkUnitClassification Classification, PriorityKeeper Keeper)
{...}
[OnDeserializing()]
private void OnDeserialize(StreamingContext s)
{...}
public PriorityKeeper PriorityKey
{...}
public bool HasError
{...}
public bool Processing
{...}
public bool Splittable
{...}
public IEnumerable<IWorkUnit> Split(int RequestedPieces, int Bonds)
{...}
public void Merge(IResponse finishedjob)
{...}
public ReadOnlyCollection<IWorkUnit> Children
{...}
public bool IsMasterJob
{...}
public Guid MasterJobID
{...}
public Guid ID
{...}
public Guid ParentID
{...}
public EnumPriority Priority
{...}
public void ChangePriority(EnumPriority priority)
{...}
public string ErrorLog
{...}
public IMetaInfo MetaData
{...}
public IJobPayload GetProcessingInfo()
{... }
public IResponseGenerator GetResponseGenerator()
{... }
}
,我測量它需要創建對象的總時間
DateTime starttime = DateTime.Now;
var test = new WorkUnit(temp, JobID, m_JobLogger);
double finished = (DateTime.Now - starttime).TotalMilliseconds;
,我一直得到以下性能數字 -
構造的時間 - 47毫秒
對象的創建時間 - 387毫秒
47 ms是可以接受的,387真的很差。取出日誌會忽略改變這些數字。有誰知道爲什麼這麼長時間?我的系統是VS 2008 SP1,面向Windows XP上的.NET 3.5 SP1。我將不勝感激任何解釋。我很快就會分析這個剖析器,但我覺得它無法深入到我需要解釋這種行爲的層面。謝謝你的幫助。
編輯:我在發佈運行
如果沒有'try'和'catch'代碼中的代碼,就很難猜測。也許你可以發佈更多的代碼?是否拋出異常(斯諾克)? – 2009-11-11 18:14:37
您是在測量第一次調用特定代碼路徑還是平均調用SUBSEQUENT?請記住,對任何事物的第一次調用可能會更加昂貴,因爲它需要加載它所引用的所有DLL的傳遞閉包,在它使用的類型的靜態構造函數中運行所有代碼,然後jit編譯所有不是代碼的代碼。首次下載代碼路徑通常比每個後續的調用都要昂貴得多。 – 2009-11-11 18:20:04