简体   繁体   English

反复呼唤 - 编码实践

[英]repeated calling - coding practice

which one do you prefer? 你更倾向哪个? (of course getSize doesn't make any complicated counting, just returning member value) (当然getSize不做任何复杂的计数,只返回成员值)

void method1(Object & o)
{
    int size = o.getSize();

    someAction(size);
    someOtherAction(size);
}

or 要么

void method2(Object & o)
{
    someAction(o.getSize());
    someOtherAction(o.getSize());
}

I know I can measure which one is faster but I want some comments... Not just executing time related... eg. 我知道我可以测量哪一个更快但我想要一些评论......不只是执行时间相关...例如。 if you are prefer method2, how many times maximally do you use o.getSize and what is the number what make you use method1 way? 如果你更喜欢method2,你最多使用o.getSize多少次,你使用method1方式的数字是多少? Any best practices? 任何最佳做法? (imagine even different types then int) TY (想象甚至不同类型然后int)TY

I would go for method 1 not just because it's probably marginally faster, but mostly because it means I don't have to worry about whether the called method has any side effects. 我会选择方法1,不仅因为它可能稍微快一点,而且主要是因为这意味着我不必担心被调用的方法是否有任何副作用。

Also, if this is called in a multi-threaded program this ensures that I'm always using my value of size - otherwise it might have changed between the two calls. 此外,如果在多线程程序中调用它,这可以确保我总是使用的大小值 - 否则它可能在两次调用之间发生了变化。 Of course there may be cases where you explicitly might want to notice that change, in which case use method 2. 当然,在某些情况下,您可能明确地想要注意到该更改,在这种情况下使用方法2。

(and yes, per other answers, make size a const int to ensure that it's not modified if it's passed by reference to something else). (是的,根据其他答案,将size设为const int以确保在通过引用其他内容传递时不会修改它)。

Since you don't want size to change when you call someAction() or someOtherAction() (as it cannot when it is the return value of a function), consider: 由于您不希望在调用someAction()someOtherAction()时更改size (因为它不是函数的返回值时),请考虑:

void method3(const Object& o)
{
    const int size = o.getSize();

    someAction(size);
    someOtherAction(size);
}

getSize() may be simple, or it may be doing a costly calculation. getSize()可能很简单,或者可能正在进行昂贵的计算。 Also, the size of o may be changed by another thread between your calls to someAction() and someOtherAction() . 此外, o的大小可能会被调用someAction()someOtherAction()之间的另一个线程更改。

I would prefer the first approach. 我更喜欢第一种方法。 Calling a function repeatedly doesn't seem good to me, especially if the returned value is same everytime. 重复调用函数对我来说似乎并不好,特别是如果返回的值每次都相同。 The first approach also avoids the overhead of calling a function repeatedly. 第一种方法还避免了重复调用函数的开销。

When I call a function that returns something multiple times (about more than 2-3 times) I usually save the returned value in a local variable. 当我调用一个多次返回多次(大约2-3次)的函数时,我通常将返回的值保存在局部变量中。 That's because I appreciate program speed more than memory saving. 那是因为我更喜欢节目速度而不是节省内存。 Not that memory wouldn't be important. 不是说记忆不重要。 It just depends on the situations. 这取决于具体情况。 Calling a function that doesn't take a lot of time to execute isn't time consuming, but a function with several loops being called a large number of times would send your program into long waits. 调用一个不需要花费大量时间执行的函数并不费时,但是多次调用多个循环的函数会使程序进入长时间等待状态。

The first one will eliminate unnecessary calls to a function, therefore I prefer method1() as the code also looks a bit cleaner. 第一个将消除对函数的不必要的调用,因此我更喜欢method1(),因为代码看起来也更清晰。

However, you should be aware that depending on context, they may produce different results. 但是,您应该知道,根据上下文,它们可能会产生不同的结果。 Say, if size changes in someAction(), and you use value stored in size variable, you may not get the desired result. 比如,如果someAction()中的大小发生变化,并且您使用存储在size变量中的值,则可能无法获得所需的结果。

当结果不会改变时反复调用任何函数是一种浪费,所以我总是采用第一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM