#1
  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. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    In my sig you will find a link to a writeup on performance programming that might help put things into perspective for you.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo