代码性能和大小 [英] Code performance and size

查看:109
本文介绍了代码性能和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了节省一些代码行之外,方法2在性能和/或代码空间方面是否有任何优点?

方法1

Other than saving some lines of code, does approach 2 have any merits in terms of performance and/or code space?

Approach 1

HRESULT FinalConstruct()
{
    HRESULT hr;
    hr = CoCreateInstance(CLSID_C1, ...);
    if(FAILED(hr))
    {
        return hr;
    }
    hr = CoCreateInstance(CLSID_C2, ...);
    if(FAILED(hr))
    {
        return hr;
    }		
    return S_OK;
}




方法2




Approach 2

HRESULT FinalConstruct()
{
    HRESULT hr;
    hr = CoCreateInstance(CLSID_C1, ...);
    if(SUCCEEDED(hr))
    {
        hr = CoCreateInstance(CLSID_C2, ...);
    }
    return hr;
}

推荐答案

如果要保存行,为什么还要声明hr来单独给它赋值?

第二个显然不进行检查,因此速度更快.但是您永远不会注意到差异,甚至无法测量差异,因此不必担心.编写对您来说最清晰的代码,可读代码更易于维护,而且比节省奇数时钟周期更重要.
If you want to save lines, why do you declare hr seperate to giving it a value ?

The second one obviously does less checks and so is faster. But you''d never notice the difference, or even be able to measure it, so it''s not worth worrying about. Write what seems the most legible to you, readable code is more maintainable and that matters more than saving the odd clock cycle.


这篇关于代码性能和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆