Jump to content

which one has better performance


tipforeveryone
 Share

Recommended Posts

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
Link to comment
Share on other sites

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 :D thanks

Link to comment
Share on other sites

No worries :D

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();
}

 

  • Thanks 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

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.

  • Like 1

Coding for Christ.

Link to comment
Share on other sites

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.

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...