juan_gandhi: (Default)
[personal profile] juan_gandhi
Although 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:


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

Date: 2004-05-05 11:47 am (UTC)
nine_k: A stream of colors expanding from brain (Default)
From: [personal profile] nine_k
Pure ecstasy!

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

May 2025

S M T W T F S
    1 2 3
456 7 8 9 10
11 121314151617
181920 21 222324
25262728293031

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 24th, 2025 07:43 pm
Powered by Dreamwidth Studios