• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tom_mai78101

Mystery of the Game Loop: How high can the 'n' in Big O can go?

18 posts in this topic

Here's a breakdown of a game loop I'm currently using:

while (true){
    //unprocessedTime calculations. Ignore
    while (unprocessedTime > 1){
        tick();
    }
}

tick(){
    for (i: 0->units.length){
        units[i].tick();
    }
}

//The element in units
Units.tick(){
    for (i: 0->units.length){
        //Do calculations that calculates the distances between each and every unit. For pathfinding.
    }
}

I abridged a lot of the details in my code, only leaving the nested loops above. As you can see, the level of nested loops reached 4, therefore, my game is running at O(n4).

 

People say this will cause the program to grind the CPU on older computers to an amble speed, thus slowing down the calculations. I do know doing pathfinding algorithms require extensive uses of loops, however nested they required.

 

Is this a sign of trouble? Does the fact that optimizations can only go as low as O(n4) because of the way nested loops work in the game logic?

0

Share this post


Link to post
Share on other sites

A good example for this is sorting. Quicksort is a relatively "poor" algorithm if you compare its average (and even more so its worst case) big-O to some more modern competitors. However, in practice, it has been one of the top performers (or the general-purpose top performer) for 5 decades, and it still is.

 

I like your post: It's relevant and well explained. So I'm sorry for nitpicking, but quicksort's asymptotic performance in the average case is O(n*log(n)), which is optimal. What you said is true for worst-case performance. Also, I am not sure you can say quicksort is "the general-purpose top performer" these days. Introsort gets the best of both worlds, avoiding the nasty Omega(n2) worst-case behavior and running about as fast as quicksort in the average case, which is why it's often the algorithm of choice for implementing std::sort in C++.

Edited by Álvaro
2

Share this post


Link to post
Share on other sites
I truly hate my conscription services.

Does time per frame counts as time per tick? In my game loop, instead of using elasped time, I calculated the difference between two elapsed time, then and now, and then obtain how many ticks I needed to do in order to make up for the difference.

I thought that in practice, big O counts more than big Theta. Am I truly mistakened?

If all this time, I am still not understanding this, I will stay in the For Beginner forums then.
0

Share this post


Link to post
Share on other sites


Does time per frame counts as time per tick?

 

It's whatever time matters for your particular program. By your description, it looks like time per tick is fine.

 


I thought that in practice, big O counts more than big Theta. Am I truly mistakened?

 

That doesn't make any sense, so I guess you are "mistakened", indeed. ;)

0

Share this post


Link to post
Share on other sites

So, in short, writing more than 4 nested loops, if and only if they are appropriately written, then it's acceptable?

 

No, I am afraid your short version is too short. You have gotten a lot of good information on this thread. Take some time to process it. In the meantime, write your code as clean as possible (meaning, easy to understand). If at the end of the day you determine your program is too slow, use a profiler to figure out what parts are too slow. Then work on those parts. Sometimes you will find that changing your algorithm provides some huge performance improvements. At that point all this discussion of complexity classes might make more sense.

Edited by Álvaro
2

Share this post


Link to post
Share on other sites

I'm processing them.

 

All I am trying to do from the very beginning is to try and link the relationships between complexity and nested loops, that's all there is to it. If this relationship doesn't exist, then I really don't understand how I pass my exams in Algorithms courses. angry.png  I'm not happy with the way I am.

0

Share this post


Link to post
Share on other sites

You have to look how many times each of those loops runs using variables when it depends on n, maybe add a few more variables if it depends on a few other things and multiply it. Now look if you can substitute those other variables like the w,x,y,z above with an expression that depends on n or if they are just constant. Then the ivory tower computer scientists discard most information and just keep the things that depend on n and call it O notation. Thats the fundamentals of it.

They also like to only count comparisons and/or exchanges for sorting algorithms and neglect anything else to ease their calculations.

 

The problem is what they do may have been a good enough measurement of performance 40 years ago when a memory read was 1 cycle always and a comparison+branch always took the same number of cycles. But all that discarded information is getting more valuable in real life every year, because of branch predictions (and mis-predictions with long pipelines to fill), practically free cpu cycles and less and less predictable time of memory access with many levels of caching.

 

There is also the problem of counting average case, when worst case can be much longer and/or more relevant.

It feels a bit like the popularity of quick sort in spite of its worst case behaviour is in some way influenced by its name and not only by its properties (someone needs any sorting algorithm that runs fast, looks at a book and finds something thats called "quick" or types into the search engine "quick sort algorithm" and only gets quick sort). I also find it a bit weird that often people say bubble sort was good, if you need something simple to program fast, when there is, for example, insertion sort.

There are many other sorting algorithms and for different uses one may be happier with radix sort, merge sort, bucket sort, heap sort, ...

0

Share this post


Link to post
Share on other sites
I wouldn't put O-notation purely into the realm of the ivory tower. You can get some useful knowledge from it. And if nothing else it really simplifies communication with your colleagues (like "I found out why our startup was so slow, the data loading happened in O(n²) when it could have been done in O(n)").

Of course, if you take it as the measure of be-all-end-all, then it will fail you. But then, most tools and concepts will fail you when you apply them without thinking.
2

Share this post


Link to post
Share on other sites

I'll just go ahead and point this out now, there is no algorithmic way to profile a game engine. Game engines are trees of function calls doing greatly varying amounts of work, in fact the function calls themselves can vary with different iterations depending on the kind of work they are doing. I'm not even really sure what you're trying to accomplish by giving any sort of algorithmic number to them.

 

In fact when profiling games the most literal way to determine performance is to check the time spent in each function out of a total. Big-O notation is only useful for explaining the abstract comparison between amounts of work. It doesn't say or even imply anything about what happens inside complex computation.

 

So basically it's not that algorithms are bad and you don't understand them its that you're trying to apply them to an example that they don't apply to.

0

Share this post


Link to post
Share on other sites

I don't think the analysis of the asymptotic behavior of an algorithm as a number grows is useless. For instance, if you are writing a game engine, or a library to do a specific task for games (physics, pathfinding...), you probably want to know how well it scales as the number of objects or the size of the scene grows. But if you are working on a specific game, I tend to agree with Satharis: Listen to your profiler.

0

Share this post


Link to post
Share on other sites

for( int a = 0; ++a != n; )
    for( int b = 0; ++b != n; )
        for( int c = 0; ++c != n; )
            for( int d = 0; ++d != n; )
                ; // This would be O(n^4)
for( int a = 0; ++a != w; )
    for( int b = 0; ++b != x; )
        for( int c = 0; ++c != y; )
            for( int d = 0; ++d != z; )
                ; // This is NOT O(n^4), because w!=x!=y!=z

 

This helped me immensely. And it's sad, because I can only understand programming codes and not texts and paragraphs. sad.png

0

Share this post


Link to post
Share on other sites

The complexity is actually  either O(n^3)  or more likely   O(n^2)

 

as that outer loop is infinite (just keeps going until you escape out - and program ends??) it dont count.

 

'unprocessed time' isnt specified how it works - if its an interval limiter for some periodic processing (whose processing isnt indicated here)   so this appears to be a time loop - so also shouldnt count in processing complexity  (which is usually based on processing per 'turn' of the game)

 

The processing of EVERY unit  per tick (which is some turn interval?)  and THAT processes comparing with every other unit (for distance tested for some interaction...)    that part is O(n^2)  for your brute force method.

 

((then plus whatever else gets done with that distance....    which again is  not specified))

 

---

 

Simple optimization :

 

Ive sometimes used a half-table caching where the distance calc is symetrical and I find it once between units and filled both in to the table  (distance[i][j] = distance[j]i] = calculated distance,   and use mutated loops (below) to make it O(n*n/2)  (it also skips  calculating the units distance from itself which is always 0)

for I=0  to  totalunits
   {
   Distance[I][I] = 0    // note- unit always 0 from itself
   for J=I+1  to  totalunits  // note starts at where I is 
       {
       D = calculate_distance(I,J);   // whatevere
       Distance[I][J] = Distance[J][I] = D;
       }  // end J loop
   } // end I loop


    0   1   2   3   4
0   0   *   *   *   *
1   .   0   *   *   *  these traversed and calculated
2   .   .   0   *   *
3   .   .   .   0   *
4   .   .   .   .   0
   these filled with symetrical data 

SO effectively you actually calculate half of what the simple looping does...

 

I usually use that when the majority of interactions are none (but still need testing for) and the distance winds up being the majority of the processing   (Ill use some space-partitioning if the 'unit' count goes high enough to warrant implementing it.

 

 

 

---

 

Sometimes (depending on the simulation) the interaction between the two units  can be carried out once and that half of the table needs no traversal, while other times the interactions are unsymetrical  (if the interactions are really sparse, building a list of only those within threshold is also possible - useful if game mechanics require sorting by interaction distance or somesuch)

Edited by wodinoneeye
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0