[Home]CPUTest/Code

Robo Home | CPUTest | Changes | Preferences | AllPages

Showing revision 2
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

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