Code Optimization Technique (logical AND and logical OR) - GeeksforGeeks

14 Dec, 2021
While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false. 
C++14
Python3
#include <bits/stdc++.h>using namespace std;// Function to check whether n is oddbool isOdd(int n) { return (n & 1); } // Function to check whether n is primebool isPrime(int n){    if (n <= 1)        return false;    for (int i = 2; i <= sqrt(n); i++)        if ((n % i) == 0)            return false;    return true;} int main(){    int cnt = 0, n = 10;     // Implementation 1    for (int i = 2; i <= n; i++) {        if (isOdd(i) && isPrime(i))            cnt++;    }     cnt = 0;    n = 10;     // Implementation 2    for (int i = 2; i <= n; i++) {        if (isPrime(i) && isOdd(i))            cnt++;    }}
# Function to check whether n is odddef isOdd(n):    pass # Function to check whether n is primedef isPrime(n):    pass if __name__ == '__main__':    cnt = 0; n = 10     # Implementation 1    for i in range(2,n) :        if isOdd(i) and isPrime(i):            cnt+=1          cnt = 0    n = 10     # Implementation 2    for i in range(2,n):        if isPrime(i) and isOdd(i):            cnt+=1    
Consider the above implementation:  
In implementation 1, we avoid checking even numbers whether they are prime or not as primality test requires more computation than checking a number for even/odd. Probability of a number getting odd is more than of it being a prime that’s why we first check whether the number is odd before checking it for prime.  
 
On the other hand in implementation 2, we are checking whether the number is prime or not before checking whether it is odd which makes unnecessary computation as all even numbers other than 2 are not prime but the implementation still checks them for prime.  
 
While using || (logical OR), we must put the condition first whose probability of getting true is high so that compiler doesn’t need to check the second condition if the first condition is true. 
C
Python3
#include <iostream.h> // Function to check whether n is oddbool isEven(int n); // Function to check whether n is primebool isPrime(int n); int main(){    int cnt = 0, n = 10;     // Implementation 1    for (int i = 3; i <= n; i++) {        if (isEven(i) || !isPrime(i))            cnt++;    }}
# Function to check whether n is evendef isEven(n):    pass # Function to check whether n is primedef isPrime(n):    pass if __name__ == '__main__':    cnt = 0; n = 10     # Implementation 1    for i in range(3,n) :        if isOdd(i) or not isPrime(i):            cnt+=1
As described earlier that the probability of a number being even is more than that of it being a non-prime. The current order of execution of the statements doesn’t allow even numbers greater than 2 to be checked whether they are non-prime (as they are all non-primes).  
Note: For larger inputs, the order of the execution of statements can affect the overall execution time for the program. 
amartyaghoshgfg
Algorithms
Analysis
Articles
C Language
Programming Language
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SCAN (Elevator) Disk Scheduling Algorithms
Rail Fence Cipher - Encryption and Decryption
Program for SSTF disk scheduling algorithm
Quadratic Probing in Hashing
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Time Complexity and Space Complexity
Practice Questions on Time Complexity Analysis
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)