That's more memory, isn't it?
Yes, it's likely 12 measly bytes.
A small price to pay for the productivity and quality gains that come from not having a 100-method class.
That's more memory, isn't it?
But there are still optimizations that you should do whenever possible.
For one, count down to 0 in for-loops whenever possible.
In Java, it looks like this:
[source lang="java"]for ( int i = iTotal; --i >= 0; ) {
}[/source]
This is also faster in C/C++ (many ways to compare against 0, and those instructions are smaller and faster), but in Java it is a major help. It reduces the size of the code itself significantly while also providing a faster iteration.
If the order of the loop does not matter, you should always always always do it this way.
This is also faster in C/C++ (many ways to compare against 0, and those instructions are smaller and faster), but in Java it is a major help. It reduces the size of the code itself significantly while also providing a faster iteration.
If the order of the loop does not matter, you should always always always do it this way.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
const int arraySize = 10000;
int a[arraySize];
int b[arraySize];
for(int i = 0; i < arraySize; i++)
{
a = 0;
b = 0;
}
LARGE_INTEGER li, start;
if( !QueryPerformanceFrequency(&li) )
return 0;
double freq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&start);
for(int i = 0; i < arraySize; i++)
a += i;
QueryPerformanceCounter(&li);
cout << double(li.QuadPart - start.QuadPart)/freq << endl;
QueryPerformanceCounter(&start);
for(int i = arraySize; i >= 0 ; --i)
b += i;
QueryPerformanceCounter(&li);
cout << double(li.QuadPart - start.QuadPart)/freq << endl;
return 0;
}
What do you mean with sensible design?
Also, if looping backwards is faster on a given platform, and the compiler for this platform can prove that the direction of the loop doesn't matter (or can compensate for it), the compiler probably already does it at some optimization level. This isn't exactly an arcane trick and is just as well-known as, say, xoring a register with itself to zero it.
This is also very much a premature optimization as well (in C++ at least). Reverse iteration might yield a slightly faster comparison, but is just as likely to cause an increased number of cache misses if the index is used in any way at all (even if the order does not matter semantically).
Not on my machine using C++.
Edit2: that wasn't a fair comparison, strangly enough switching the order of the iterations makes a difference
For iOS, for example, profiling revealed 2 functions to be high on the time-consuming list (Xcode comes with “Instruments” that show you how long every function takes) and I was able to reduce the time spent inside those functions by half by reversing the order of the loop to count down to 0.