C++ is one of the most efficient programming languages. Most programmers prefer C++ due to its execution speed.
<
)The <
operator in C++ compares the two values on its left and right side. If the value on the right side is greater than the value on the left side, it returns true. Otherwise, it returns false.
#include <iostream>using namespace std;int main() {int a = 10;int b = 20;int c = 20;cout <<"Is a greater than b? "<< (a < b) <<endl;cout <<"Is b greater than c? "<< (b < c)<<endl;return 0;}
<=
The <=
operator compares the two values on its left and right side. If the value on the right side is greater than or equal to the value on the left side, it returns True. Otherwise, it returns False.
#include <iostream>using namespace std;int main() {int a = 10;int b = 20;int c = 20;cout <<"Is a greater than b? "<< (a <= b) <<endl;cout <<"Is b greater than c? "<< (b <= c)<<endl;return 0;}
<
and <=
Now let's see if <
is faster than <=
in practice.
In a test, the maximum possible score was 100. Students with 51 marks or more passed the test. Given that the test score was only given in integer values, let's write the code using <
and <=
to compare if one is faster than the other.
<
int passingScore = 51;int score = 60;// Implementation using <auto start = high_resolution_clock::now();if(score < passingScore){cout<<"Failed"<<endl;}else{cout<<"Passed"<<endl;}auto stop = high_resolution_clock::now();auto duration = duration_cast<microseconds>(stop - start);cout << "Time taken using < : "<< duration.count() / 1000.<< " ms" << endl;
<=
int passingScore = 51;int score = 60;// Implementation using <=auto start = high_resolution_clock::now();if(score <= passingScore){cout<<"Failed"<<endl;}else{cout<<"Passed"<<endl;}auto stop = high_resolution_clock::now();auto duration = duration_cast<microseconds>(stop - start);cout << "Time taken using <= : "<< duration.count() / 1000.<< " ms" << endl;
We see no difference in the execution time of the operators from test 1.
Now let's suppose we have two large arrays containing random integer values from 0 to 99. Our task is to compare each value at the same index of the two arrays and increase the count if the value in first array is strictly less than the value in the second array at the corresponding index.
<
int randArray1[100000];int randArray2[100000];for(int i=0; i < 100000; i++){randArray1[i] = rand() % 100;randArray2[i] = rand() % 100;}//Implementation using <int count = 0;auto start = high_resolution_clock::now();for(int i=0; i < 100000; i++){if(randArray1[i] < randArray2[i]){count++;}}auto stop = high_resolution_clock::now();auto duration = duration_cast<microseconds>(stop - start);cout << "Time taken using < : "<< duration.count() / 1000.<< " ms" << endl;
<=
int randArray1[100000];int randArray2[100000];for(int i=0; i < 100000; i++){randArray1[i] = rand() % 100;randArray2[i] = rand() % 100;}//Implementation using <=int count = 0;auto start = high_resolution_clock::now();for(int i=0; i < 100000; i++){if(randArray1[i] <= randArray2[i]){count++;}}auto stop = high_resolution_clock::now();auto duration = duration_cast<microseconds>(stop - start);cout << "Time taken using <= : "<< duration.count() / 1000.<< " ms" << endl;
The execution time for the two operators turns out to be very similar for the second test as well.
The tests that we performed above suggest no significant evidence to say that the <
operator is faster than <=
.
Free Resources