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.
(work1 == 0)
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, ?
tempAnswer = work1 + work2 + toCarry
.
, - , .
, - , ? tempAnswer++? , .
tempAnswer++
, , - . , , .
, , . , .
, , , , , , . . , , . .
, . , .
, , "" . , - :
if (var1 != 0) someobject.property1 += var1;
, jobt1 , . :
if (var1 != 0) volatilevar2 += var1;
volatilevar2, var1 . , , , - "", . - :
if (var1 != 0) Threading.Interlocked.Add(volatilevar2, var1);
, , , .
, , , , ( ), , , CPU , , , .
, , . , . . - " " .
. , , .
, , , . , , . , tempAnswer = work1 + work2 + toCarry; .
tempAnswer = work1 + work2 + toCarry;
- . ? ? , , , , . alu , , () , , .. ARM . , , - , ? (, , ..). , , add/sub , , xor . , , . ?
, , . :
- . , , , .
- , , , . /ram, , . , , , , . , , .
- "" "", , , .
, "" , .
$ 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; } $
$ 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 . - , - .
: " ".
? , ?
, @ " ", .. . : - - , , , , / !
, xx.c yy.c: ?
!
Source: https://habr.com/ru/post/1756763/More articles:Does the Silverlight control stay on top of the div? Cannot use windowless mode, but z-index does not work - cssHow To Start Win Long Long Service With Threading - multithreadingCode-First Entity Framework - SQL CE DB creation error - entity-frameworkadding / removing a file in VS2010 leads to the error "WebDev.WebServer20.exe has stopped working" - visual-studio-2010Связывание строковой коллекции с ListView, Windows Forms - collectionsSSL / TLS / HTTPS sites are very slow in the C # /. NET WebBrowser control, but work fine in Internet Explorer - c #Deleting an entity strategy - javaCalibrate Alsa buffers correctly, weird API - cWhat are some useful keyboard shortcuts in Visual Studio? - c #How to save JTable columns when resizing while resizing JFrame? - javaAll Articles