加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#对象创建比构造函数调用慢得多

发布时间:2020-12-15 19:54:59 所属栏目:百科 来源:网络整理
导读:对于我的生活,我无法弄清楚我的代码中的性能影响.我有一个容器对象,我在其中测量运行构造函数(下面的对象)所需的时间,公共构造函数中的计时代码 public class WorkUnit : IWorkUnit{ private JobInformation m_JobInfo; private MetaInfo m_MetaInfo; privat
对于我的生活,我无法弄清楚我的代码中的性能影响.我有一个容器对象,我在其中测量运行构造函数(下面的对象)所需的时间,公共构造函数中的计时代码

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毫秒是可以接受的,387真的很糟糕.取出记录可以忽略不计地改变这些数字.有谁知道为什么这么长时间?我的系统是VS 2008 SP1,针对Windows XP上的.NET 3.5 SP1.我将不胜感激任何解释.我将很快打破探查器,但我觉得它无法深入研究解释这种行为所需的水平.谢谢你的帮助.

编辑:我正在发布

解决方法

你确定你所看到的是对象创建时间而不是CLR启动的效果吗?

尝试在循环中运行测试50次并忽略第一个结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读