Thread: efficiency of a programming

  1. #1
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13

    Post efficiency of a programming

    when we consider efficiency of a programme, what is the main fact we have to consider about?? Is it Using less variables(Memory) or using less loops. I know we have to consider both. BUt what is the most important fact??
    Last edited by sureshhewa; 08-01-2008 at 07:43 PM. Reason: readability

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Space (memory) and time (I'm assuming that's what you're getting at with the loops) are not comparable. So it doesn't make sense to ask which is most important. (It's like asking which is more important: one acre of land or one hour of time. It doesn't make sense.)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by sureshhewa View Post
    when we consider efficiency of a programme, what is the main fact we have to consider about?? Is it Using less variables(Memory) or using less loops. I know we have to consider both. BUt what is the COLOR="Red"]most important[/COLOR] [/COLOR]fact??
    The most important thing is usually time complexity. That means fewer iterations. Counting loops is not accurate; it is how many times an index of an array is accessed, as a function of the length of a the array.

    Complexity does not usually matter outside arrays and other data structures, since everything else is only done once.

    You want your algorithms to access the each element in an array/data structure as few times as possible. This does not mean that calling array[i] two lines in a row is bad, just that you should avoid accessing every element in the array to get to the one you need, for example.

    Besides that there is nothing that you need to look out for in terms of efficiency, unless you actually notice a problem. Then you want to investigate the cause and address it at the source.

    Addendum:
    Using lots of variables does not mean using more memory. There is no efficiency loss from using lots of variables-- the memory you program uses remains the same regardless. Variables are allocated on the stack and that never grows or shrinks in memory uses. Memory depends only on calls to malloc, calloc, realloc, and free, for these are the functions that give extra memory to your process.
    Last edited by King Mir; 08-01-2008 at 08:04 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13

    Post

    ok. think of it this way. now i have to write a programme. I have 2 strategies to solve it.
    1.I can use an extra structre array which will allocate160bytes
    2. I can put nested loop which will be loop 100 times

    so what should i choose. I know it is depend on the problem. When a good programmer face to this matter what are the facts he/she must consider?? any rough idea??

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by tabstop View Post
    Space (memory) and time (I'm assuming that's what you're getting at with the loops) are not comparable. So it doesn't make sense to ask which is most important. (It's like asking which is more important: one acre of land or one hour of time. It doesn't make sense.)
    Sure it does. I prefer my programs to run fast, but do not mind having to buy a few gigs of ram for it. Furthermore there is virtual memory if that is not enough.

    These days memory is cheap, but in time we want everything to seem instantaneous. Yet using too much memory becomes a cost in time: the computer starts to use virtual memory which is slower.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13
    thanks. i got your point

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by sureshhewa View Post
    ok. think of it this way. now i have to write a programme. I have 2 strategies to solve it.
    1.I can use an extra structre array which will allocate160bytes
    2. I can put nested loop which will be loop 100 times

    so what should i choose. I know it is depend on the problem. When a good programmer face to this matter what are the facts he/she must consider?? any rough idea??
    Time means that you can do something quickly.
    Memory means that you can do a lot of things at once.
    In cases where you want both, you have to compromise. But the choices then depend directly on how much you want each of those two things; if you want to do things quickly, prefer fewer loops; if you want to do many things at once, prefer less memory.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In some cases, if your code complexity is already optimized pretty well but your code it huge, you could speed up your program by making it smaller. If all or most of your code can fit into the CPU cache, it will reduce the need to access RAM which is slower than cache.
    Most modern compilers are smart enough to choose the right blend of size/speed optimizations to run in the best way for your CPU.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficiency with c++
    By pastitprogram in forum C++ Programming
    Replies: 17
    Last Post: 08-08-2008, 11:18 AM
  2. Calculating space efficiency using sizeof()
    By markcls in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 05:25 AM
  3. Efficiency problems
    By Crazy Glue in forum C# Programming
    Replies: 15
    Last Post: 07-20-2006, 08:38 AM
  4. Programme Efficiency
    By Cikotic in forum C Programming
    Replies: 3
    Last Post: 06-28-2004, 01:29 PM
  5. Binary tree search efficiency
    By ExCoder01 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-23-2003, 10:11 PM