tipforeveryone Posted September 20, 2018 Share Posted September 20, 2018 I am optimizing my game's frame per sec and wonder which way of code has better performance function Script:WithManyCalculation() --A ton of code end function Script:UpdateWorld() self:WithManyCalculation() end and function Script:Start() self.time = Time:GetCurrent() end function Script:WithManyCalculation() --A ton of code end function Script:UpdateWorld() if Time:GetCurrent() - self.time > 60 then self:WithManyCalculation() self.time = TIme:GetCurrent() end end Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted September 20, 2018 Share Posted September 20, 2018 I would think that the simple checking of a few variables would have a small effect compared to executing a lot of code in your function. It depends also on what type of code your executing, but the later would be more efficient. It's also what I do in C++ to gain better performance. 1 Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted September 20, 2018 Author Share Posted September 20, 2018 3 minutes ago, SpiderPig said: I would think that the simple checking of a few variables would have a small effect compared to executing a lot of code in your function. It depends also on what type of code your executing, but the later would be more efficient. It's also what I do in C++ to gain better performance. Yes that function WithManyCalculation have many thing inside thanks Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted September 20, 2018 Share Posted September 20, 2018 No worries Also, (not sure if LUA is the same) but even things as simple as declaring a variable out side of a large loop can help. for(int id=0;id<1000;id++){ Vec3 pos = entity->GetPosition(); } The above will be slower than the loop below; Vec3 pos; for(int id=0;id<1000;id++){ pos = entity->GetPosition(); } 1 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 20, 2018 Share Posted September 20, 2018 1 hour ago, SpiderPig said: declaring a variable out side of a large loop can help Are you sure about this? I was under the impression (I could be wrong) that compilers optimized things like this. For example, pretty sure if you write code like x = y + 2 + 5, it will optimize it to x = y + 7. Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted September 20, 2018 Share Posted September 20, 2018 Pretty sure. I know there are optimizations made by the compiler (I'm not sure exactly what they are) but making that change to a few of my large loops made a massive difference. Mainly for Vec3 variables. Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 20, 2018 Share Posted September 20, 2018 Generally, I'm reading the opposite, to the extent that some people say it's outright better: https://stackoverflow.com/questions/7959573/declaring-variables-inside-loops-good-practice-or-bad-practice https://stackoverflow.com/questions/982963/is-there-any-overhead-to-declaring-a-variable-within-a-loop-c/982999 The only exception I read (in just some quick searches) was if the constructor and/or destructor did some work, which may be the case for Vec3. 1 Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted September 20, 2018 Share Posted September 20, 2018 You're probably right. I've only seen a big speed increase with Vec3 so it must be something in the constructor. Unless I've manged to somehow turn off optimizations 1 Quote Link to comment Share on other sites More sharing options...
Genebris Posted September 21, 2018 Share Posted September 21, 2018 You are asking if you should run complicated code every frame or every minute? I think it doesn't mater. If your game runs at 100 FPS most of the time but drops to 30 every minute it's just as bad as constant 30 FPS. Unless you do your calculations on loading screens, of course. Quote Link to comment Share on other sites More sharing options...
Slastraf Posted September 24, 2018 Share Posted September 24, 2018 So you need something big calculated, but not in real time and in the background. Even if you calculate it every 60. frame , there will be lag on that frame, because the renderer is going to wait for the calculation to finish (most likely). If you use c++ I urge you to do efficient Multithreading. Make a second thread (not in this forum lol) and let it run in the background, for a few frames with less lag , and if its finished it passes some arguments back to the main program. Maybe a list of vectors to make many Ai agents go toward or something. If you want to use Lua only, and no extra c++, then I would resketch your program (part) and split the huge calculation into smaller ones. Its probably not the processor who is too slow, you just have inefficient code. 1 Quote Link to comment Share on other sites More sharing options...
catch22 Posted November 9, 2018 Share Posted November 9, 2018 I'm a nodejs software engineer. When dealing with this stuff you generally design the routines to be "chunkable"; like you can do pieces of the work every 10ms or whatever, so it's non-blocking to the rest of the thread. Alternatively, you can fork child processes to do that, and use IPC (interprocess communication) to poll results and what not. Since you're really being quite vague there's no easy answer. But if you're asking if you should be doing "big calculations" when the game happens to be running over 60fps, I think there's probably a better way to design your program flow; it's certainly not a 'standard' way of looking at things. It seems weird to me you'd have "big" stuff to do then, and only then, rather than tasks that just need to be done all the time, or potentially all the time, whenever, wherever, etc.. Anonymous function processing is a thing as well for small bursts of stuff you don't really have anyway of knowing if it's going to be happening in regular enough intervals to write classes/functions for it -- ie: like doing a lamba on a separate thread -- but these are specific kind of workloads, lest you need to think about mutex/resource contention. C++ on modern hardware is extremely fast. You'd be surprised how much it can do at interactive framerates; if you're experiencing slow downs you need to profile your app and see where your bottlenecks are and address them accordingly. 1 Quote Coding for Christ. Link to comment Share on other sites More sharing options...
Josh Posted November 10, 2018 Share Posted November 10, 2018 When I looked at this I assumed there would be many instances of this object with many calculations, and you would want to space them out in a way so that just one got calculated each frame. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.