Big O - 检查方法然后返回方法与将方法分配给变量并返回变量?

Big O - Checking Method and then returning method vs assigning method to variable and returning variable?

大 O - 检查方法然后返回方法与将方法分配给变量并返回变量?

A​​ 和 B 之间的运行时间是否不同。据我所见,我首先将 peek 分配给一个变量并重新使用它 (O(n))。在第二个中,我 运行 peek() 两次,这会使它变慢 (O(n^2))。我对么?

甲:

Object peek = stack.peek();
if (peek != null) {
    return peek;
}

else {
    return null;
}

乙:

if (stack.peek() != null) {
    return stack.peek();
}

else {
    return null;
}

将相同的操作执行两次而不是一次会使运行时间加倍。这在渐近复杂度分析中被视为"constant multiplier",并被忽略。