- import junit.framework.TestCase;
-
- public class FloatVsDoublePerfomance extends TestCase {
- private static final int[] SIZES = new int[]{ 1000, 10000, 100000, 300000 };
- private static final int ITER = 10000;
-
- public void test() {
- for ( int i = 0; i < SIZES.length; i++ ) {
- final int size = SIZES[i];
-
- _testFloat( size );
- System.gc();
- System.gc();
-
- _testDouble( size );
- System.gc();
- System.gc();
- }
- }
-
- private static void _testFloat( final int size ) {
- final float[] arr = createFloatArray( size );
-
- System.out.printf( "float[%d * %d]\n", size, ITER );
- //warm-up -- cache load, etc
- testFloatCompare( arr );
-
-
- long startedAt = System.currentTimeMillis();
- //compare
- for ( int i = 0; i < ITER; i++ ) {
- testFloatCompare( arr );
- }
- System.out.printf( "\tcompare: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //summ
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testFloatSumm( arr );
- }
- System.out.printf( "\tsum: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //mul
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testFloatMul( arr );
- }
- System.out.printf( "\tmul: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //div
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testFloatDiv( arr );
- }
- System.out.printf( "\tdiv: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
- }
-
- private static void _testDouble( final int size ) {
- final double[] arr = createDoubleArray( size );
-
- System.out.printf( "double[%d * %d]\n", size, ITER );
- //warm-up -- cache load, etc
- testDoubleCompare( arr );
-
-
- long startedAt = System.currentTimeMillis();
- //compare
- for ( int i = 0; i < ITER; i++ ) {
- testDoubleCompare( arr );
- }
- System.out.printf( "\tcompare: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //summ
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testDoubleSumm( arr );
- }
- System.out.printf( "\tsum: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //mul
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testDoubleMul( arr );
- }
- System.out.printf( "\tmul: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
-
- //div
- startedAt = System.currentTimeMillis();
- for ( int i = 0; i < ITER; i++ ) {
- testDoubleDiv( arr );
- }
- System.out.printf( "\tdiv: %d ms\n", ( System.currentTimeMillis() - startedAt ) );
- }
-
- private static float[] createFloatArray( final int size ) {
- final float[] arr = new float[size];
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = ( float )( Math.random() * 100 );
- }
- return arr;
- }
-
- private static void testFloatDiv( final float[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] / arr[i];
- }
- }
-
- private static void testFloatMul( final float[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] * arr[i];
- }
- }
-
- private static void testFloatSumm( final float[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] + arr[i];
- }
- }
-
- private static void testFloatCompare( final float[] arr ) {
- int _ = 0;
- for ( int i = 0; i < arr.length; i++ ) {
- if ( arr[i] < 50 ) {
- _++;//просто чтобы компилятор не выкинул сравнение
- }
- }
- }
-
-
- private static double[] createDoubleArray( final int size ) {
- final double[] arr = new double[size];
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = Math.random() * 100;
- }
- return arr;
- }
-
- private static void testDoubleDiv( final double[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] / arr[i];
- }
- }
-
- private static void testDoubleMul( final double[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] * arr[i];
- }
- }
-
- private static void testDoubleSumm( final double[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- arr[i] = arr[i] + arr[i];
- }
- }
-
- private static void testDoubleCompare( final double[] arr ) {
- int _ = 0;
- for ( int i = 0; i < arr.length; i++ ) {
- if ( arr[i] < 50 ) {
- _++;//просто чтобы компилятор не выкинул сравнение
- }
- }
- }
- }
* This source code was highlighted with Source Code Highlighter.
тестировал под 1.6.0_14, с опциями -server -XX:+DoEscapeAnalysis -Xmx300m -Xms300m
В итоге, получается, что сложение/умножение примерно в два раза медленнее для double, деление -- примерно на 30%, сравнение мало отличается. Буду теперь для операций, не требующих особой точности, активно использовать float.
Комментариев нет:
Отправить комментарий