Hello R3krishnan,
As noted by biophysicist, it is probably "best" to write a program
that does it both ways and measure the time. The program at the end
does this for four cases:
Case 1 - b>a, ?: syntax
Case 2 - a>b, ?: syntax
Case 3 - b>a, if syntax
Case 4 - a>b, if syntax
I ran this program without optimization (but the system DID have some
other tasks active) and got a variety of different results
maniac% ./a
Case 1: Elapsed time of ?: for 2 is 126470
Case 2: Elapsed time of ?: for 1 is 108352
Case 3: Elapsed time of if for 2 is 123058
Case 4: Elapsed time of if for 1 is 126946
maniac% ./a
Case 1: Elapsed time of ?: for 2 is 154794
Case 2: Elapsed time of ?: for 1 is 265825
Case 3: Elapsed time of if for 2 is 134012
Case 4: Elapsed time of if for 1 is 225877
maniac% ./a
Case 1: Elapsed time of ?: for 2 is 125807
Case 2: Elapsed time of ?: for 1 is 46714
Case 3: Elapsed time of if for 2 is 51381
Case 4: Elapsed time of if for 1 is 47666
maniac% ./a
Case 1: Elapsed time of ?: for 2 is 84825
Case 2: Elapsed time of ?: for 1 is 121256
Case 3: Elapsed time of if for 2 is 77436
Case 4: Elapsed time of if for 1 is 85672
As you can see - it is important to have a relatively idle system to
get accurate results. However, based on the third and fourth runs - it
appears the variation of run time is not significant [one odd result,
the other three were basically the same].
However, I do suggest you repeat the tests on your system. Adjust
ILOOPS and JLOOPS if needed to get reasonable run times and accuracy.
Make a clarification request if you have problems building the
application or if the answer is otherwise unclear.
--Maniac
#include <sys/time.h>
struct timeval tv1,tv2;
#define JLOOPS 100
#define ILOOPS 1000000
int main() {
int i, j;
int a, b, c, d;
int x;
int tmin, dt;
c = 1;
d = 2;
a = 1;
b = 2;
tmin = 2000000000;
for (j=0; j<JLOOPS; j++) {
gettimeofday(&tv1, NULL);
for (i=0; i<ILOOPS; i++) {
x = (a>b)?c:d;
}
gettimeofday(&tv2, NULL);
dt = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
if (dt<tmin)
tmin = dt;
}
printf("Case 1: Elapsed time of ?: for %d is %d\n", x, tmin);
a = 2;
b = 1;
tmin = 2000000000;
for (j=0; j<JLOOPS; j++) {
gettimeofday(&tv1, NULL);
for (i=0; i<ILOOPS; i++) {
x = (a>b)?c:d;
}
gettimeofday(&tv2, NULL);
dt = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
if (dt<tmin)
tmin = dt;
}
printf("Case 2: Elapsed time of ?: for %d is %d\n", x, tmin);
a = 1;
b = 2;
tmin = 2000000000;
for (j=0; j<JLOOPS; j++) {
gettimeofday(&tv1, NULL);
for (i=0; i<ILOOPS; i++) {
if (a>b)
x = c;
else
x = d;
}
gettimeofday(&tv2, NULL);
dt = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
if (dt<tmin)
tmin = dt;
}
printf("Case 3: Elapsed time of if for %d is %d\n", x, tmin);
a = 2;
b = 1;
tmin = 2000000000;
for (j=0; j<JLOOPS; j++) {
gettimeofday(&tv1, NULL);
for (i=0; i<ILOOPS; i++) {
if (a>b)
x = c;
else
x = d;
}
gettimeofday(&tv2, NULL);
dt = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
if (dt<tmin)
tmin = dt;
}
printf("Case 4: Elapsed time of if for %d is %d\n", x, tmin);
return 0;
} |