I am writing an exchange system in Java, but I seem to have run into a crippling performance issue.
The following code is where execution becomes VERY slow:
outer:
for(ArrayList<Offer> listSell : sellOffers.values()) {
for(Offer sellOffer : listSell) {
ArrayList<Offer> listBuy = buyOffers.get(getStorageHash(sellOffer));
if(listBuy == null)
continue outer;
for(int i = 0; i < listBuy.size(); i++) {
Offer buyOffer = listBuy.get(i);
//todo - handle exchange
}
}
}
After looking deeper into this, I found that the following line seems to be the issue:
Offer buyOffer = listBuy.get(i);
If I change this line to the following, it will no longer cripple execution time:
Object buyOffer = listBuy.get(i);
Therefore, when casting on the object from listBuy there is a major delay in execution. Is there any work around for this? I'm stumped.
Thanks in advance!
for(Offer sellOffer : listSell)) to plain for loop (for (int i=0; i<listBuy.size(); i++))? Why not use foreach on the third loop too?outer) is discouraged, and should only be used where needed. You don't need it, because usingbreakwill get you same result.