Study reflective properties, reflecting the assignment and the assignment set method comparison

We may have a vague idea, reflecting slower performance, but how slow is no specific data. I'll write code to test.

package com.itbac.reflection;

import java.lang.reflect.Field;

public class test {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        long begin = System.currentTimeMillis();
        Book book = new Book();
        book.setName ( "the Java from entry to give up" );
        book.setNum ( 1 );
        book.setPrice(1.2);
        System.out.println(book.toString());
              // loop one million times 
        for ( int I = 0; I <10000000; I ++ ) {
             // 1.set assignment method takes milliseconds:. 7
 //             book.setNum (I ++);

            // 2. Reflector assignment ms Processed: 1045 
            setFeild (Book, I, "NUM" );
        }
        System.out.println(book.toString());
        long end = System.currentTimeMillis();
        System.out.println ( "ms:" + (END- the begin));
    }

    private static void setFeild(Book book, int i,String str) throws NoSuchFieldException, IllegalAccessException {
        Class<? extends Book> aClass = book.getClass();
        Field num = aClass.getDeclaredField(str);
        num.setAccessible(true);
        num.set(book, i);
    }
}

Through the above tests, I found that when circulating the assignment of 10 million cycles, reflection takes one second. This slow to be felt by humans. If your reflexes call, less than one million times, please do not say the slow reflex.

Look comparison of the two methods, can be directly SET assignment method, the method further reflection and needs to obtain Class Field, set access rights, several unit operation. I could use a map to optimize it.

package com.itbac.reflection;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class test {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        long begin = System.currentTimeMillis();
        Book book = new Book();
        book.setName ( "the Java from entry to give up" );
        book.setNum ( 1 );
        book.setPrice(1.2);
        System.out.println(book.toString());
        Map <String, Field,> = Map new new the HashMap <> ();
               // loop one million times 
        for ( int I = 0; I <10000000; I ++ ) {
             // 1.set assignment method ms Processed: 7
 //             book.setNum (++ i);

            // 2. Assignment reflection (with no map) ms Processed: 1045
             // 3. reflection assignment (using map cache Field) ms Processed: 95 
            setFeild (Book, I, "NUM" , map);
        }
        System.out.println(book.toString());
        long end = System.currentTimeMillis();
        System.out.println ( "ms to:" + (END- the begin));
    }

    private static void setFeild(Book book, int i,String str,Map<String, Field> map) throws NoSuchFieldException, IllegalAccessException {
        Field field = map.get(str);
        if (null == field) {
            Class<? extends Book> aClass = book.getClass();
            field= aClass.getDeclaredField(str);
            field.setAccessible(true);
            map.put(str, field);
        }
        field.set(book, i);
    }
}

This treatment, a draw data, 95 divided by 7 is approximately equal to 13.5, concluded that a million times to call, reflecting the assignment 13.5 times slower than direct assignment.

However, reflecting the assignment 10 million cycles, it takes 95 milliseconds, is people can not perceive speed. That we can be accepted by the program code run faster.

I tried to increase the number of reflections, to see how many times we feel that in order to be slow it.

Code results are as follows:

One hundred million times reflection assignment, time-consuming ms: 687

Two hundred million reflection assignment, time-consuming millisecond: 1468

 

Personal conclusion:

  Reflection assignment is slower than direct assignment, but less than one hundred million times you call, you do not feel it is a slow, safe to use.

Of course, set the assignment method is more perverted, 200 million assignment, took 15 milliseconds. They are able to do reflection, as far as possible without reflection, it is not, use is also possible.

Guess you like

Origin www.cnblogs.com/itbac/p/11963086.html