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??
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
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.)
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.
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??
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.
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.
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.