Reputation: 42100
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
?
Upvotes: 1
Views: 435
Reputation: 21
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.
Upvotes: 2
Reputation: 25723
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)
Upvotes: 2