How to fix awesome lags? lazarus / 61-Beginners ---------------------------------------------------------------------- DiCri · 2016-08-28 16:04 I am creating a small game like an advanced version of the dinosaur game of google chrome while the user is not connected to the internet. Ok, While i was creatig that game, some lags appeared so i fixed them. But this time, the lag is very eccessive(bad english). Without the function i putted, the game doesn't work very well. I think the problem is this where there are the conditions  checking if the dino touches the cactus: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Die;        begin                if up = false then begin               for i4 := 1 to 100 do begin                cactus1^.x := CCactus[i4];                if ( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w ) or                   ( dino123rect^.x + dino123rect^.w >= cactus1^.x ) and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w ) then                      if ( dino123rect^.y >= cactus1^.y ) and ( dino123rect^.y <= cactus1^.y + cactus1^.h ) or                           ( dino123rect^.y + dino123rect^.h >= cactus1^.y ) and ( dino123rect^.y + dino123rect^.h <= cactus1^.y + cactus1^.h ) then death := true;              writeln( death );                end;                 end;      end;Here is it the full game : http://manueldicriscito.altervista.org/GoogleGame.zip ---------------------------------------------------------------------- Thaddy · 2016-08-28 16:19 If you use Boolean logic, why the intermediate "If..then"? I do not understand that. That should be an AND operation too.... Agree There is no logic in that, especially since Boolean shortcut evaluation will bail out at any point. Continuing in Boolean style instead of forcing a jump might help. Write it so that all boolean truths are satisfied up until the determination of dead. This should show an improvement: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i               for i4 := 1 to 100 do begin                cactus1^.x := CCactus[i4];                if (( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w ) or                   ( dino123rect^.x + dino123rect^.w >= cactus1^.x ) and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w )) and                   (( dino123rect^.y >= cactus1^.y ) and ( dino123rect^.y <= cactus1^.y + cactus1^.h ) or                   ( dino123rect^.y + dino123rect^.h >= cactus1^.y ) and ( dino123rect^.y + dino123rect^.h <= cactus1^.y + cactus1^.h )) then death := true;  You should also look at impossibilities in your algorithm, btw. It may be more efficient to bring one or more  variables outside of the loop. ---------------------------------------------------------------------- Handoko · 2016-08-28 16:30 @Manu12x I can't be sure to say it will work. But I think it may help a bit. You code tests all the positions to know if the player hit the cactus, if yes then it die. It can be optimized by checking if it the player is outside the cactus position and stop to test to current cactus but move to the next loop. How it can speed up the performance? Because most of the time, the player is alive, so by checking not dying and continue to the next loop is better. You use continue command the quit the the testing and move to the next looping. As usual I won't provide the code, but a draft of the concept: Code: [Select]for i = 1 to something do begin   CurrentCactus := Cactus[i];   if (PlayerX < CurrenctCactus.x) then continue;   if (PlayerX > CurrenctCuctus.x + CactusWidth) then continue;   ... The code still can be further improved if you know how. More info about continue command: http://www.freepascal.org/docs-html/rtl/system/continue.html ---------------------------------------------------------------------- Thaddy · 2016-08-28 16:36 Quote from: Handoko on August 28, 2016, 06:30:44 pm@Manu12x I can't be sure to say it will work. But I think it may help a bit. No. He needs to do what I wrote and bring variables that are constant (it seems at first glance half of them are known values  *before* the loop is entered...) inside the loop outside of the loop and remove the if/then and replace it with full boolean logic. Normally your advice is very helpful. This is, however, about speed. In the future FPC can do this kind of optimization by itself. There is work being done in that direction. OTOH you are right that he can test for NOT, but in the case of boolean shortcut evaluation that is pretty much the same. ---------------------------------------------------------------------- Handoko · 2016-08-28 16:58 Quotebring variables that are constant inside the loop outside This is what I meant about the 'can be further improved'. @Manu12x Speed optimization is an art that may need years to learn. Most of the tricks can be learned from books and reading other programmers' code. But the best teacher is learning from experience. Usually if I meet something similar to your case, I will add some codes to do performance benchmark and test to optimize the code or probably using different solutions to find out which one is the best. Now, there already has 3 suggestions, so if you want to improve your programming skill especially on performance optimization, please test all the suggestions to find out which one is the best. Some or all of them may combined. 1. Move some calculations inside the loop to prior of the loop (by Thaddy) 2. Avoid jumping (if-then-else) but use full boolean (by Thaddy) 3. Use continue command to skip the current loop and do the next (by me) @Thaddy I'm not sure he will understand how to do the things we said. ---------------------------------------------------------------------- Thaddy · 2016-08-28 17:00 He will learn I will add example for outside the loop: for OP: it simply means you are making calculations every time you loop (100 times) when these only need to be calculated once. Later I will show you what I mean. [edit] Like so:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // declare intermediate variables with values that don't change inside the loopva r DinoXw:Integer; DinoYh:Integer; CactusXw:integer; CactusYh:Integer;  .......                DinoXw := dino123rect^.x + dino123rect^.w ; // saves 99 calculations                DinoYh :=dino123rect^.y + dino123rect^.h;  // saves another 99 calculations                for i4 := 1 to 100 do begin                cactus1^.x := CCactus[i4];              CactusYh := cactus1^.y + cactus1^.h;  // Saves just a bit              CactusXw := cactus1^.x + cactus1^.w ; //Saves just a bit                   ------  You can now substitute all these calculations with a variable that already has the correct value. In a loop that is a HUGE speed gain. ---------------------------------------------------------------------- DiCri · 2016-08-28 18:12 Ok i'm not good at English so i will write in italian language so please translate it from google or other. Ok. Del consiglio che @Thaddy mi ha dato, ne sono consapevole ma preferivo nel mio modo così da organizzarmi meglio, e poi provandoci, non trovavo molte differenze inserendo AND. Poi, il comando "CONTINUE" non l' ho mai sentito e magari da questo potrei imparare qualcosa. E poi se qualche volta non capisco le risposte che mi date, la causa è il mio pessimo inglese. Io proverò e vi dirò se tutto è andato bene, grazie per la comprensione e le risposte!! ---------------------------------------------------------------------- DiCri · 2016-08-28 18:31 OK i tried!!! Thank you @Handoko you helped me a lot reducing the code a lot using the continue command too. Thanks again. See you ( not really see ) next time! ---------------------------------------------------------------------- shobits1 · 2016-08-28 23:30 This: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w )or ( dino123rect^.x + dino123rect^.w >= cactus1^.x )and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w )is logically equal to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ( dino123rect^.x + dino123rect^.w >= cactus1^.x )and ( dino123rect^.x <= cactus1^.x + cactus1^.w ) the same goes for dino123rect^.y .... (the Y function); so, you could cut the calculation by more than half (in the worst case) and also moving dino123rect^.x + dino123rect^.w out of the loop could help too. PS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo. ---------------------------------------------------------------------- Handoko · 2016-08-29 03:30 Quote from: shobits1 on August 29, 2016, 01:30:25 amPS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo. 100% agree. ---------------------------------------------------------------------- DiCri · 2016-08-29 13:01 Quote from: Handoko on August 29, 2016, 05:30:37 amQuote from: shobits1 on August 29, 2016, 01:30:25 amPS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo. 100% agree. Really? Thanks