Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is faster?

Tags:

c++

If we have the following 2 snippets of code in c++ that do the same task:

int a, b=somenumber;
while(b > 0)
{
a = b % 3;
b /= 3;
}

or

int b=somenumber;
while(b > 0)
{
int a=b%3;
b /= 3;
}

I don't know much about computer architecture/c++ design, but i think that the first code is faster because it declares the integer a at the beginning and just uses it in the while-loop, and in the second code the integer a is being declared everytime the while-loop starts over. Can some one help me with this, am i correct or what and why ?

like image 893
VaioIsBorn Avatar asked Mar 30 '10 15:03

VaioIsBorn


People also ask

What do you mean by faster?

moving or happening quickly, or able to move or happen quickly: fast cars. a fast swimmer. Computers are getting faster all the time.

What kind of word is faster?

Word Type. Faster can be a noun, an adverb or an adjective.

Is faster a correct word?

Explanation: We need the comparative form here, faster, because we're comparing two things. If we were talking about more than two drives, we'd use the superlative form, fastest. Those are the rules for positive (fast), comparative (faster) and superlative (fastest) adverbs and adjectives.

What is the meaning of faster and faster?

at an increasing rate of speed; fast and then even faster. The car went faster and faster and I was afraid we would crash.


1 Answers

There should be no difference, but to be extra empirical (anal?) I tested this with g++, creating a function for each of the code snippets. Both with and without optimizations it generated identical code no matter where the int a declaration is.

#include <iostream>

int variant_a(int b)
{
        int a;
        while(b > 0)
        {
                a = b % 3;
                b /= 3;
        }
        return b;
}

int variant_b(int b)
{
        while(b > 0)
        {
                int a = b % 3;
                b /= 3;
        }
        return b;
}

int main()
{
        std::cout << variant_a(42) << std::endl;
        std::cout << variant_b(42) << std::endl;
}

This is the unoptimized loop:

_Z9variant_ai:
.LFB952:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $24, %esp
.LCFI2:
        jmp     .L2
.L3:
        movl    8(%ebp), %eax
        movl    %eax, -20(%ebp)
        movl    $1431655766, -24(%ebp)
        movl    -24(%ebp), %eax
        imull   -20(%ebp)
        movl    %edx, %ecx
        movl    -20(%ebp), %eax
        sarl    $31, %eax
        subl    %eax, %ecx
        movl    %ecx, %eax
        addl    %eax, %eax
        addl    %ecx, %eax
        movl    -20(%ebp), %edx
        subl    %eax, %edx
        movl    %edx, %eax
        movl    %eax, -4(%ebp)
        movl    8(%ebp), %eax
        movl    %eax, -20(%ebp)
        movl    $1431655766, -24(%ebp)
        movl    -24(%ebp), %eax
        imull   -20(%ebp)
        movl    %edx, %ecx
        movl    -20(%ebp), %eax
        sarl    $31, %eax
        movl    %ecx, %edx
        subl    %eax, %edx
        movl    %edx, %eax
        movl    %eax, 8(%ebp)
.L2:
        cmpl    $0, 8(%ebp)
        jg      .L3
        movl    8(%ebp), %eax
        leave
        ret

and the optimized one:

_Z9variant_ai:
.LFB968:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        pushl   %ebx
.LCFI2:
        movl    8(%ebp), %ebx
        testl   %ebx, %ebx
        jle     .L2
        movl    $1431655766, %ecx
        .p2align 4,,7
        .p2align 3
.L5:
        movl    %ebx, %eax
        imull   %ecx
        movl    %ebx, %eax
        sarl    $31, %eax
        movl    %edx, %ebx
        subl    %eax, %ebx
        jne     .L5
.L2:
        movl    %ebx, %eax
        popl    %ebx
        popl    %ebp
        ret
like image 78
Jakob Borg Avatar answered Oct 06 '22 04:10

Jakob Borg