Jump to content
  • Advertisement
Sign in to follow this  
Dynamo_Maestro

What is a tight loop?

This topic is 1834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

A tight loop is a loop that loops many times and the loop body has few instructions. 

 

Thanks for the quick reply

 

Well that's what I thought but then I came across http://stackoverflow.com/questions/2212973/what-is-a-tight-loop on the accepted answer

 

 

(computing) Such a loop which heavily uses I/O or processing resources, failing to adequately share them with other programs running in the operating system.

 

It made me think of RenderLoop() in C# (SharpDX), wouldn't this make RenderLoop a tight loop?

 

See atm the situation im in is, I am using C# for everything in my graphics apps except math / physics calculations which is from a C++ library with C++/CLI marshalling, one way or another Update() calls these math / physics functions, if Update() is part of a tight loop (based on what I said above) wouldn't this mean I am doing it wrong as I am calling the interop methods within a tight loop, I mean eventually everything is called in Update() / Render() anyway as they are part of the RenderLoop so I wouldn't even know how to work around this

 

I hope I made sense here, I cant help but feeling I am doing things wrong or completely misunderstanding the SO answer, I would post code but its nothing more than me calling C++ methods in RenderLoop() in C#

 

Thanks in advance

Share this post


Link to post
Share on other sites

Tight loops are to do with performance, they don't necessarily have few instructions inside of it, they are however time critical to the application so you want to eliminate operations from them that can be dealt with elsewhere.

Share this post


Link to post
Share on other sites

if you're worried about performance, measure it.

 

measure the execution speed of your code comparing the code that calls the unmanaged methods with code that call managed methods that do the same calculations.

 

if you're so worried about performance that you care about tight loops, you should really consider if you want to pay the marshaling cost.

Share this post


Link to post
Share on other sites


wouldn't this mean I am doing it wrong?

Does it work?

 

For a game, nobody really cares if your system is sub-optimal. Nobody cares about the implementation details. Nobody cares that you chose one algorithm over another.  All that people care about is it looks good on their screen and plays well in their mind.

 

If they say "It looks like a game, and it is fun" they don't care which pathfinding algorithm you used, or if your particle systems rely on SOA or AOS layouts.  All they care about is that the end result looks good and feels good to them.

 

 

If it works for you and your app, then it is just fine.

 

Computers are really, really, really fast.  For the PC you've got about a half-billion cycles per frame. Feel free to use them.

Share this post


Link to post
Share on other sites

I feel I may have given the impression my concern was performance this isn't at all the case, it was truly more of a "am I doing it right?", I could probably get away with having Thread.Sleep(##) everywhere and still not notice speed differences. What really concerned me was calling native methods in C# but within a 'tight' loop, when I usually see rules, practices and principles I take notice of them, like not using macros in C++ for example. I probably should have made it obvious that my concern wasn't performance.

 

@ frob, I agree 100% with what you said, sadly this isn't a game or even game related and without going into too much detail this all will be looked at, I don't mind breaking some traditions, I just don't want to get in a habit of doing things bad now or wrong, like C++ macro's, where later I have to change, ofc it is understandable that this may inevitably happen at times.

 

Thanks for the responses regardless :)

Share this post


Link to post
Share on other sites



SiCrane, on 09 Aug 2013 - 09:26 AM, said:
A tight loop is a loop that loops many times and the loop body has few instructions. 
 
Thanks for the quick reply
 
Well that's what I thought but then I came across
 
Quote
 
(computing) Such a loop which heavily uses I/O or processing resources, failing to adequately share them with other programs running in the operating system.
 

 

as usual, the gamedev world, and the rest of the world are on different pages. 

 

in the gamdev world the first definition is correct, and speaks to the small size (tightness) and hence the speed of the loop. rasterization would be a typical place to find a tight loop, although one could be found anywhere fast iterations are required or occur naturally.

 

to the rest of the world, the second definition is the understood meaning, and speaks to the fact that the loop can be a resource hog while running.

 

the difference stems from two very different world views: games should be fast, vs software doesn't have to run fast and therefore should timeshare resources. 

 

many times the difference in world views between game development and other software development, movie making, storytelling, the manufacturing industry, business theory, etc are large. it tends to lead to a lot of confusion re: terminology, as well as some mis-application of inappropriate methodologies from outside the gamedev industry to game development.

Share this post


Link to post
Share on other sites


the difference stems from two very different world views: games should be fast, vs software doesn't have to run fast and therefore should timeshare resources.

I'm not sure I have heard that definition since preemptive multitasking became the norm...

 

A small loop that performs an I/O operation strikes me as the exact opposite of a "tight" loop. Pretty much by definition, your loop will spend most of your time blocked on I/O, which frees up the CPU for everyone else.

Share this post


Link to post
Share on other sites

I would say that the term 'tight loop' is rather colloquial really, there probably isn't a single widely recognised definition so I wouldn't worry too much about "the right thing" when it comes to such an informal concept.

 

For me a tight loop conjures up the idea that it's a high-frequency loop with a lot of looping to do, it might therefore consume a lot of CPU resource and take a while to complete. In a real-time application like a game that would usually make them time-critical (needs to be as fast as possible or it will end up being too slow otherwise) which tends lead to them being small.

 

Sometimes a tight loop is actually undesirable yet it would be the natural consequence of a naive implementation. For example, say you're processing messages off an event-queue; messages don't come through all the time. You could sit there spinning away in a (tight) loop checking for messages, but that will waste a lot of compute time; instead better to slacken off that loop with a sleep or wait.

Edited by dmatter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!