C optimization question

I am wondering what is the fastest way to write code. I have a loop that does an add in some int. The loop will run many, many times, so I thought about making comparisons to check if any operands are equal to zero, so they cannot be considered added, as shown below:

if (work1 == 0)
{
    if (work2 == 0)
        tempAnswer = toCarry;
    else
        tempAnswer = work2 + toCarry; 
}
else if (work2 == 0)
    tempAnswer = work1 + toCarry;
else
    tempAnswer = work1 + work2 + toCarry;

I believe that the nested IF at the top is already an optimization, since it is faster than recording a series of comparisons with && 's, as I would check (work1 == 0)more than once.

Unfortunately, I could not say how often work1 and work2 will be zero, so suppose this is probably a balanced distribution of each possible result of the IF statement.

, , , tempAnswer = work1 + work2 + toCarry, ?

+3
9

.

  • , .
  • , ( , , (. ), ).
  • , - , .

    , - , ? tempAnswer++? , .

+26

, , - . , , .

, , . , .

+17

, , , , , , . . , , . .

+2

, . , .

+2

, , "" . , - :

  if (var1 != 0)
    someobject.property1 += var1;

, jobt1 , . :

  if (var1 != 0)
    volatilevar2 += var1;

volatilevar2, var1 . , , , - "", . - :

  if (var1 != 0)
    Threading.Interlocked.Add(volatilevar2, var1);

.

, , , .

+1

, , , , ( ), , , CPU , , , .

, , . , . . - " " .

. , , .

, , , . , , . , tempAnswer = work1 + work2 + toCarry; .

+1

- . ? ? , , , , . alu , , () , , .. ARM . , , - , ? (, , ..). , , add/sub , , xor . , , . ?

, , . :

tempAnswer = work1 + work2 + toCarry;

- . , , , .

- , , , . /ram, , . , , , , . , , .

+1

- "" "", , , .

, "" , .

$ cat yy.c
int optimizable(int work1, int work2, int toCarry)
{
    int tempAnswer;
    if (work1 == 0)
    {
        if (work2 == 0)
            tempAnswer = toCarry;
        else
            tempAnswer = work2 + toCarry; 
    }
    else if (work2 == 0)
        tempAnswer = work1 + toCarry;
    else
        tempAnswer = work1 + work2 + toCarry;

    return tempAnswer;
}
$ cat xx.c
int optimizable(int work1, int work2, int toCarry)
{
    int tempAnswer;
    tempAnswer = work1 + work2 + toCarry;
    return tempAnswer;
}
$

Compiler

$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     86       0       0      86      56 xx.o
    134       0       0     134      86 yy.o
$ gcc -O -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     54       0       0      54      36 xx.o
     71       0       0      71      47 yy.o
$ gcc -O1 -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     54       0       0      54      36 xx.o
     71       0       0      71      47 yy.o
$ gcc -O2 -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     54       0       0      54      36 xx.o
     70       0       0      70      46 yy.o
$ gcc -O3 -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     54       0       0      54      36 xx.o
     70       0       0      70      46 yy.o
$ gcc -O4 -c yy.c xx.c
$ size xx.o yy.o
   text    data     bss     dec     hex filename
     54       0       0      54      36 xx.o
     70       0       0      70      46 yy.o
$

64- RedHat Linux AMD x86-64.

(3 , 1 , 1 ). 16 . - , - .

+1

: " ".

? , ?

, @ " ", .. . : - - , , , , / !

, xx.c yy.c: ?

!

0

Source: https://habr.com/ru/post/1756763/


All Articles