简体   繁体   中英

How to optimize this kind of code in Java?

I need to optimize some Java code, which "converts" ~100 POJOs of one type to POJOs of another type.

Suppose, for example, there are POJOs A1 and A2 . and there is a function A2 convert(A1 a) , which creates a new instance of A2 and copies data from the given a to it. Suppose also I have a function Collection<A2> convert(Iterator<A1> as) , which works with collections of ~100 objects.

How can I make this convert run faster. Can I optimize the new instance creation in convert ?

You might want to parallelize it to use CPU efficiently.

Another way is to make convert faster. A combination of the two techniques will give you faster code.

On the other hand you can try to use some sort of inheritance between A1 and A2 and just cast it(this is from guess work, I don't know how your code is exactly)

You can get drastic improvements when you do multiple conversions in parallel. Java has great features for this. Take a look at the Thread class to get started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM