1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0

#### Efficiency in Code

Hello fellow forumers. I recently started getting into programming but ran into 2 dilemmas.

1. How is that I can determine when it is more efficient either int or unsigned int?

2. I was told to design a program that determines whether a triangle is impossible, equilateral, isoceles, or scalene given 3 sides. I basically came up with two possible programs that could help me determine this. Now my question is that which of the 2 is more efficient?

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int a,b,c;
int x,y;
scanf("%d%d%d", &a,&b,&c);
x=(a+b+c)/2;
y=x*(x-a)*(x-b)*(x-c);
if (y<=0)
{
printf("Impossible \n");
}
else
{
if (a==b && b==c)
{
printf("Equilateral \n");
}
else if ((a==b) || (b==c))
{
printf("Isoceles \n");
}
else
{
printf("Scalene \n");
}
}
return 0;
}```
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int a,b,c;
scanf("%d%d%d", &a,&b,&c);
if((c>a+b) || (b>a+c) || (a>b+c))
{
printf("Impossible \n");
}
else
{
if (a==b && b==c)
{
printf("Equilateral \n");
}
else if ((a==b) || (b==c))
{
printf("Isoceles \n");
}
else
{
printf("Scalene \n");
}
}
return 0;
}```
Thanks for the help in advance :)
2. > How is that I can determine when it is more efficient either int or unsigned int?
There is probably so little difference that you'll find it hard to measure. At the machine level, there is only one set of instructions for add, mul etc.

If you really want to study what your compiler does
\$ gcc -S foo.c
\$ more foo.s

> Now my question is that which of the 2 is more efficient?
Well if you exclude the printf and scanf as being the totally dominant factors, then just go through and count all the +, * and comparisons.
3. In my sig you will find a link to a writeup on performance programming that might help put things into perspective for you.