Thursday, July 12, 2018

java - Why do separate loops run faster than one loop that does multiple things?


My game had some lag, so I tried to improve the performance.


I found that if I write my loops separately like this:


for (...){
for (...){

// Do operation A
// (Fill int[][] with 1s)
}
}

for (...){
for (...){
// Do operation B
// (Work with Sprite Renderers & Spritebatch)
}

}

for (...){
for (...){
// Do operation C
// (Work with Shape Renderers with alpha)
}
}

then the result runs a whole lot faster than if I try to do all the operations in one loop:



for (...){
for (...){
// Do operation A
// Do operation B
// Do operation C
}
}

Can anyone explain this?


I'm running this game on Android OS!




Answer



After some deep research and a lot of testing finally found out that the problem wasnt the loop as because it works on a simple maths. but the problem was in the rendering batch, the more batch there is, java will likely to share more memory to each batch as because batch is a collection of vertices. i divided them because i have to batch.begin() and batch.end() every batch, and it runs better. thanks all in helping me today. P.s sorry for bas english XD


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...