Which one works faster?

Mani Kazemi

Do these 2 have any differences?

if (condition)
{
    std::cout << "Condition is true";
}
else
{
    std::cout << "Condition is false";
}

OR

if (condition)
{
    std::cout << "Condition is true";
    return 0;
}
std::cout << "Condition is false";

I know that is not good to use the second one because maybe you have some more code after it. But at the end of the code that we don't have anything after that else, isn't it better to use the second one?

Shao-Chuan Wang

I modified the original question a bit as follows,

void f1() {
    int a = 0;
    if (a > 0) {
        a = 1;
    } else {
        a = 2;
    }
}

void f2() {
    int a = 0;
    if (a > 0) {
        a = 1;
        return;
    } 
    a = 2;
}

Here are the compiled assembly. f1() and f2() are nearly identical.

f1():
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], 0
        cmp     DWORD PTR [rbp-4], 0
        jle     .L2
        mov     DWORD PTR [rbp-4], 1
        jmp     .L4
.L2:
        mov     DWORD PTR [rbp-4], 2
.L4:
        nop
        pop     rbp
        ret
f2():
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], 0
        cmp     DWORD PTR [rbp-4], 0
        jle     .L6
        mov     DWORD PTR [rbp-4], 1
        jmp     .L5
.L6:
        mov     DWORD PTR [rbp-4], 2
.L5:
        pop     rbp
        ret

The only difference is that .L4 has a nop. More details can be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Which one is faster

Which one will run faster

Which one is faster numpy or pandas?

Order of execution and which one is faster

Which one is faster? Regex or EndsWith?

Which one is faster for object detection?

Which one of these two concurrent implementations is a better faster

numpy.max or max ? Which one is faster?

numpy.max or max ? Which one is faster?

Which one is faster? for-loop or isEqualToArray

C++ expressions - which one of the two is faster?

Which one is faster? Array Initialization or SIMD operations?

Which one is faster "find" or "at" in unordered_map?

Spark coalesce vs collect, which one is faster?

Which one is faster and better? Is this compare method true?

Mongoose Which one is faster .populate() or .find()

Which one is faster/optimized if (a==false) or if (a != true)

Watson Speech To Text service works faster for which type of audio file?

Which one is faster? List.contains() or Map.containsKey()

Which one is faster x+=x or x*=2

Which one of these two fields results in faster database query?

Which is faster, copying everything at once or one thing at a time?

Awk, tail, sed or others - which one faster for big files?

Using Distinct or Group by in SQL . Which one is better and faster? State it

Python 3 - Which one is faster for accessing data: dataclasses or dictionaries?

Which one is faster for querying Athena: pyathena or boto3?

Which one is faster between map, collect, select and pluck?

Which is faster: multiple single INSERTs or one multiple-row INSERT?

Which one is faster get_headers() vs curl ()?