Google Answers Logo
View Question
 
Q: C Prog ( Answered,   2 Comments )
Question  
Subject: C Prog
Category: Computers > Programming
Asked by: r3krishnan-ga
List Price: $2.00
Posted: 03 Feb 2005 15:11 PST
Expires: 05 Mar 2005 15:11 PST
Question ID: 468332
In C, is using the "?:" syntax (eg. x = (a>b)?c:d) faster than
if a > b {
   c
else 
   d
Answer  
Subject: Re: C Prog
Answered By: maniac-ga on 03 Feb 2005 17:37 PST
 
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;
}
Comments  
Subject: Re: C Prog
From: biophysicist-ga on 03 Feb 2005 16:08 PST
 
You can figure this out for yourself.  Write a loop to do this a
million times.  For example,

a=1;
b=2;
for (i=0; i<1000000; i++)
{
  if (b<a)
    x=1;
  else
    x=2;
}

and likewise for the "?:" operator.  Run them both.  See which takes
longer.  If they both run really quickly, keep increasing the number
of iterations (say, by a factor of 10 each time) until they take long
enough that you can measure the time easily.
Subject: Re: C Prog
From: frde-ga on 04 Feb 2005 06:08 PST
 
I think your question really translates into :-

  Is terse code faster than longer code that does the same thing ?

The answer to that is (and this comes as a surprize to relatively
'new' programmers) - generally not

The C compiler translates the C code into ASM 
(talking Intel here, but the principles generally apply)
It is the structure of the ASM that affects speed

In this case even a half brained compiler would almost certainly
produce identical code.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy