How to write more efficient code

Discussion in 'C' started by DemiSheep, Aug 28, 2010.

  1. DemiSheep

    DemiSheep New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Question of the century? I basically want to know which would be more efficient if I wrote this code as several different variables or if I used small arrays.
    int x = 34;
    int y = 28;
    int z = 293;vs
    double coordinate[3] = {34, 28, 293};I have a coordinate struct which I will use in the following way:

    typedef struct coordinates_t {
    double x = 0.0;
    double y = 0.0;
    double z = 0.0;

    } coordinates;


    typedef struct car_t {
    coordinates start; // car starting point
    coordinates location; // car current Location
    coordinates targCarVector; // Vector to car from target
    coordinates altitude; // Altitude of car
    coordinates distance; // Distance from car start to current position
    } car;I'll need to do things like:
    distance = car1.location - car1.start;If I don't use an array I'll have to have a lot of lines of code, but if I use an array I'll have to use loops. Are arrays and loops more memory/cpu intensive? I am basically trying to see which is the most efficient way of writing this code.

    Thanks,
    DemiSheep
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice