[Home]CPUTest/Code

Robo Home | CPUTest | Changes | Preferences | AllPages

Showing revision 4
public class CPUTest{
        private final static int APPROXIMATE_CYCLES_ALLOWED = 30000;
        private final static long TEST_PERIOD_NANOSECS = 5000000000L; // 5 seconds

        public static void main(String[] args){
                for(int i = 0; i < 10000; i++){//gets the random() initialized and optimized by the jvm
                        double d = Math.random()*Math.random();
                }
                double avgGran = 0;
                int maxGran = 0;
                int delta = 0;

                // Run several times, so we find the most representative granularity
                // Sometimes, e.g. 1 out of 100 times we get a lower granularity.
                for (int i = 0, max = 1000; i < max; i++) {
                        long time = System.currentTimeMillis();

                        while ((delta = (int) (System.currentTimeMillis() - time)) == 0);

                        avgGran += delta/(double)max;
                        maxGran = Math.max(maxGran,delta);
                }
                System.out.println("Average granularity: " + avgGran);
                System.out.println("Max granularity over 1000 'granulations': " + maxGran);


                long start = System.nanoTime();
                long count = 0;
                double d = -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.atan(Math.random());
                        count++;
                }

                long atanConstant = APPROXIMATE_CYCLES_ALLOWED * TEST_PERIOD_NANOSECS / count;
		System.out.println("atan constant: " + atanConstant/1000000.0);

                start = System.nanoTime();
                count = 0;
                d *= -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.sqrt(Math.random());
                        count++;
                }

                long sqrtConstant = (long)(APPROXIMATE_CYCLES_ALLOWED * 1.3 * TEST_PERIOD_NANOSECS / count);
		System.out.println("sqrt constant: " + sqrtConstant/1000000.0);

                start = System.nanoTime();
                count = 0;
                d *= -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.pow(Math.random(), Math.random());
                        count++;
                }

                long powConstant = (long)(APPROXIMATE_CYCLES_ALLOWED * 0.58 * TEST_PERIOD_NANOSECS / count);
		System.out.println("pow constant: " + powConstant/1000000.0);

		start = System.nanoTime();
                count = 0;
                d *= -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.hypot(Math.random(), Math.random());
                        count++;
                }

                long hypotConstant = (long)(APPROXIMATE_CYCLES_ALLOWED * 0.4 * TEST_PERIOD_NANOSECS / count);
		System.out.println("hypot constant: " + hypotConstant/1000000.0);

		start = System.nanoTime();
                count = 0;
                d *= -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.random()/Math.random();
                        count++;
                }

                long divisionConstant = (long)(APPROXIMATE_CYCLES_ALLOWED * 1.1 * TEST_PERIOD_NANOSECS / count);
		System.out.println("division constant: " + divisionConstant/1000000.0);

		start = System.nanoTime();
                count = 0;
                d *= -1;

                while (System.nanoTime() - start < TEST_PERIOD_NANOSECS && d != -d) {
                        d += Math.random()*Math.random();
                        count++;
                }

                long multiplicationConstant = (long)(APPROXIMATE_CYCLES_ALLOWED * 1.1 * TEST_PERIOD_NANOSECS / count);
		System.out.println("multiplication constant: " + multiplicationConstant/1000000.0);

		
		long finalConstant = Math.max(Math.max(Math.max(atanConstant,sqrtConstant),Math.max(divisionConstant,multiplicationConstant)),Math.max(powConstant,hypotConstant));
		
		System.out.println("Final constant: " + finalConstant + " nanoseconds, " + finalConstant/1000000.0 + " milliseconds");


        }
}

Here's what it gives me:

Average granularity: 1.015999999999999
Max granularity over 1000 'granulations': 15
atan constant: 30.868783
sqrt constant: 32.062354
pow constant: 31.511018
hypot constant: 32.038326
division constant: 30.805312
multiplication constant: 30.748685
Final constant: 32062354 nanoseconds, 32.062354 milliseconds
-- Skilgannon

Exactly what I did in my function time and fnl did before me for the original nanotime code, unlike me however you changed it all into constants. --Chase-san

I think the most important thing that I added is to have the Math.max(all possible constants) at the end. On some JVMs, and any other JVMs in the future, certain functions may take more time than others, or the proportions may change. Because we want a bot to be able to spend it's full allocated time in any possible function, it wouldn't really be fair to average them together, since say the one function the bot uses the most happens to be the 'slow' function on that system, it will cause the bot to skip turns. I you take a look, I had to 'scale' a few functions, for example Math.hypot *0.4, otherwise it would blow the whole thing way out of proportion. I'm wondering what this same test gets on other setups. It's also interesting to note that the granularity on my system, for the most part, was 1 millisecond. There was probably only one or two instances of the 15 granularity in the whole 1000 iterations. -- Skilgannon


Robo Home | CPUTest | Changes | Preferences | AllPages
Edit revision 4 of this page | View other revisions | View current revision
Edited February 12, 2008 20:36 EST by Skilgannon (diff)
Search: