apply Moore's law to calculate buffer size
May. 2nd, 2004 01:09 amAlthough it does not happen so often these days, even less to Java programmers, but from time to time one has to decide how much to allocate for this or that buffer. It is known that a typical buffer size changes with time; it is also known that software can live much longer than its creators can imagine.
Do you want to clog your program with very small buffers (that were okay five years ago)?
There is a solution: use a version of Moore's law. Computers memory doubles every two years (see also http://www.firstmonday.dk/issues/issue7_11/tuomi/#t6).
So that, next time you allocate a buffer, you can make it dependent on current time. Here's the method:
Use it like this:
This way the size is recalculated every time the class is loaded; in the beginning of 2004 it is 64K; it doubles by 2006, according to the formula in the method:
This of course makes more sense for the code that will work for many years - but you never know these days (and years).
Do you want to clog your program with very small buffers (that were okay five years ago)?
There is a solution: use a version of Moore's law. Computers memory doubles every two years (see also http://www.firstmonday.dk/issues/issue7_11/tuomi/#t6).
So that, next time you allocate a buffer, you can make it dependent on current time. Here's the method:
private final static double aYear = (double)1000 * 60 * 60 * 24 * (365 * 4 + 1) / 4;
public static final int adjustSizeByMooreLaw(int size, int thisYear) {
double milli = System.currentTimeMillis();
double q = Math.exp((milli / aYear + 1970 - thisYear) / 2 * Math.log(2));
return (int)(size * q);
}
Use it like this:
static final int MAX_BUFFER_SIZE = adjustSizeByMooreLaw(64000, 2004);
This way the size is recalculated every time the class is loaded; in the beginning of 2004 it is 64K; it doubles by 2006, according to the formula in the method:
- get current time (milliseconds);
- divide it into the number of milliseconds in a year;
- add 1970 and subtract the year you write this code, getting N, number of years passed;
- calculate 2 to the power of (N/2) - this is the Moore quotient;
- multiply your "development time size" by the quotient
This of course makes more sense for the code that will work for many years - but you never know these days (and years).