对于我的生活,我无法弄清楚我的代码中的这种性能。我有,我测量它需要多长时间运行构造的容器对象(对象下方),在公共构造计时码现在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