Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Console App how to populate a 10.000*10.000 Grid efficiently?

Console App how to populate a 10.000*10.000 Grid efficiently?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++efficiencyqt c++
14 Posts 4 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 28 Jun 2024, 15:49 last edited by
    #4

    Why would you loop through the array? Do you want to search something?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • S StudentScripter
      28 Jun 2024, 15:39

      @JonB QVector is fine for the coordinates, for the grass area im going with qbytearray and bool values, seems to be faster. But still looping trough this huge as array is the main bottleneck. I thought of parallelizing it in different thread, each thread a chunk of the array.

      J Offline
      J Offline
      JonB
      wrote on 28 Jun 2024, 16:04 last edited by
      #5

      @StudentScripter
      Ummm, if you have a "huge" array (whatever) of data and you "loop through it" then it's going to be "slow". What else would you expect? You have something like 100 million squares of 10x10 somethings. I'm with @Christian-Ehrlicher: what are you trying to do, why, how often, is there a more efficient way of doing it (e.g. indexing, lookup tables)? Either this is inherently a very slow exercise or there is something "sub-optimal" about the approach.

      You can indeed split the search across threads, but just how much difference will that make? How many free cores/CPUs will your target have? If, say, as many as 4 cores are used it might reduce the search by a factor of up to 4. How much real difference will that make to performance? OTOH reducing the map or the search to, say, 1,000x1,000 would reduce by a factor of 100. If the search could be "indexed" then even more.

      S 1 Reply Last reply 28 Jun 2024, 16:44
      0
      • J JonB
        28 Jun 2024, 16:04

        @StudentScripter
        Ummm, if you have a "huge" array (whatever) of data and you "loop through it" then it's going to be "slow". What else would you expect? You have something like 100 million squares of 10x10 somethings. I'm with @Christian-Ehrlicher: what are you trying to do, why, how often, is there a more efficient way of doing it (e.g. indexing, lookup tables)? Either this is inherently a very slow exercise or there is something "sub-optimal" about the approach.

        You can indeed split the search across threads, but just how much difference will that make? How many free cores/CPUs will your target have? If, say, as many as 4 cores are used it might reduce the search by a factor of up to 4. How much real difference will that make to performance? OTOH reducing the map or the search to, say, 1,000x1,000 would reduce by a factor of 100. If the search could be "indexed" then even more.

        S Offline
        S Offline
        StudentScripter
        wrote on 28 Jun 2024, 16:44 last edited by
        #6

        @JonB Well let me explain.

        Some Function "GrassChanger" determines which patches will be changed, it stores all squares that shall be changed in a vector.

        The squares changed can be anywhere in this big map, at the beginning only a few hunderd ones will be changed, but this amout will grow over time, eventually reaching many millions of squares needed to be changed at onces.

        Of course i thought of an mechanism to invert this search at the point where one side becomes > 50% to save resources. How would i do indexed search with Qbitarray with the values from the vector? I asume something like:

        (Pseudocode)
        
        vector[100000]
        
        QBitArray ba(5000000);
        
        for(int i; i < vector.size; i++){
        
        ba.togglebit(i);
        }
        

        Reducing the map smaller is of course an option, but nothing i would like to do as the precision of the calculations depend highly on having a huge samplesize eg a lot of numbers.

        Let me know what you think, im grateful for any better solution/optimisation idea. :) <3

        J 1 Reply Last reply 28 Jun 2024, 17:48
        0
        • S StudentScripter
          28 Jun 2024, 16:44

          @JonB Well let me explain.

          Some Function "GrassChanger" determines which patches will be changed, it stores all squares that shall be changed in a vector.

          The squares changed can be anywhere in this big map, at the beginning only a few hunderd ones will be changed, but this amout will grow over time, eventually reaching many millions of squares needed to be changed at onces.

          Of course i thought of an mechanism to invert this search at the point where one side becomes > 50% to save resources. How would i do indexed search with Qbitarray with the values from the vector? I asume something like:

          (Pseudocode)
          
          vector[100000]
          
          QBitArray ba(5000000);
          
          for(int i; i < vector.size; i++){
          
          ba.togglebit(i);
          }
          

          Reducing the map smaller is of course an option, but nothing i would like to do as the precision of the calculations depend highly on having a huge samplesize eg a lot of numbers.

          Let me know what you think, im grateful for any better solution/optimisation idea. :) <3

          J Offline
          J Offline
          JonB
          wrote on 28 Jun 2024, 17:48 last edited by JonB
          #7

          @StudentScripter
          So here you toggle 100,000 bits, right? Which takes whatever time it takes (and what is that?) If that is what you need to do I don't see how that can be optimized in itself. There may be better low-level ways of toggling the bit-states in a larger number of adjacent words, I don't know. You may find quite a difference compiling for Release if you are currently Debug.

          I did do a simulation a while ago with a large domain and simple operations,. I did parallelize it with QThread, but I had few cores/CPUs available. Obviously that approach is proportionate to what the end machine will have available for your use.

          S 1 Reply Last reply 28 Jun 2024, 17:50
          0
          • J JonB
            28 Jun 2024, 17:48

            @StudentScripter
            So here you toggle 100,000 bits, right? Which takes whatever time it takes (and what is that?) If that is what you need to do I don't see how that can be optimized in itself. There may be better low-level ways of toggling the bit-states in a larger number of adjacent words, I don't know. You may find quite a difference compiling for Release if you are currently Debug.

            I did do a simulation a while ago with a large domain and simple operations,. I did parallelize it with QThread, but I had few cores/CPUs available. Obviously that approach is proportionate to what the end machine will have available for your use.

            S Offline
            S Offline
            StudentScripter
            wrote on 28 Jun 2024, 17:50 last edited by
            #8

            @JonB Thanks yeah i guess as much threads with a strong enough machine is what im going to try. :) Don't see any other option aswell. Thanks alot regardless.

            J 1 Reply Last reply 28 Jun 2024, 17:53
            0
            • S StudentScripter has marked this topic as solved on 28 Jun 2024, 17:51
            • S StudentScripter
              28 Jun 2024, 17:50

              @JonB Thanks yeah i guess as much threads with a strong enough machine is what im going to try. :) Don't see any other option aswell. Thanks alot regardless.

              J Offline
              J Offline
              JonB
              wrote on 28 Jun 2024, 17:53 last edited by
              #9

              @StudentScripter
              Before you start with threading make sure all your time really is being spent in the toggling or whatever action. Not for example any GUI or waiting! Profile if you can. And make sure you really are only doing what you show, whatever is right of the heart of the loop should be lean and mean.

              S 1 Reply Last reply 28 Jun 2024, 18:18
              0
              • J Online
                J Online
                jeremy_k
                wrote on 28 Jun 2024, 18:01 last edited by
                #10

                Depending on the pattern of changes, a quadtree may be a more efficient structure in terms of storage space and execution time.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • J JonB
                  28 Jun 2024, 17:53

                  @StudentScripter
                  Before you start with threading make sure all your time really is being spent in the toggling or whatever action. Not for example any GUI or waiting! Profile if you can. And make sure you really are only doing what you show, whatever is right of the heart of the loop should be lean and mean.

                  S Offline
                  S Offline
                  StudentScripter
                  wrote on 28 Jun 2024, 18:18 last edited by
                  #11

                  @JonB @jeremy_k

                  It actually no gui at all till now. Only 4 spinboxes or so to enter some values. Also: I switched from debug to release build and now even with 1000.000.000 numbers and without threading it does perform in nearly realtime.

                  I will have some "entities" that will be able to move in one direction at a time. Could a quadtree be there a good solution?
                  Originaly thought of using a 2d vector storing x and y pos of the "entities" (they arent visual, just position numbers) and updating them by looping trough the vector but as said probably not the best idea. Is a quadtree more efficient in updating all members positions and "collision"?

                  J J 2 Replies Last reply 28 Jun 2024, 18:55
                  0
                  • S StudentScripter has marked this topic as unsolved on 28 Jun 2024, 18:18
                  • S StudentScripter
                    28 Jun 2024, 18:18

                    @JonB @jeremy_k

                    It actually no gui at all till now. Only 4 spinboxes or so to enter some values. Also: I switched from debug to release build and now even with 1000.000.000 numbers and without threading it does perform in nearly realtime.

                    I will have some "entities" that will be able to move in one direction at a time. Could a quadtree be there a good solution?
                    Originaly thought of using a 2d vector storing x and y pos of the "entities" (they arent visual, just position numbers) and updating them by looping trough the vector but as said probably not the best idea. Is a quadtree more efficient in updating all members positions and "collision"?

                    J Offline
                    J Offline
                    JonB
                    wrote on 28 Jun 2024, 18:55 last edited by JonB
                    #12

                    @StudentScripter
                    Yes I would expect this kind of code to show a good difference in release vs debug.

                    I have no idea about your "quadtree" since I have never heard of one :) Sounds like the kind of tree a quadbike would run into?

                    1 Reply Last reply
                    1
                    • S StudentScripter
                      28 Jun 2024, 18:18

                      @JonB @jeremy_k

                      It actually no gui at all till now. Only 4 spinboxes or so to enter some values. Also: I switched from debug to release build and now even with 1000.000.000 numbers and without threading it does perform in nearly realtime.

                      I will have some "entities" that will be able to move in one direction at a time. Could a quadtree be there a good solution?
                      Originaly thought of using a 2d vector storing x and y pos of the "entities" (they arent visual, just position numbers) and updating them by looping trough the vector but as said probably not the best idea. Is a quadtree more efficient in updating all members positions and "collision"?

                      J Online
                      J Online
                      jeremy_k
                      wrote on 29 Jun 2024, 15:53 last edited by
                      #13

                      @StudentScripter said in Console App how to populate a 10.000*10.000 Grid efficiently?:

                      @JonB @jeremy_k
                      I will have some "entities" that will be able to move in one direction at a time. Could a quadtree be there a good solution?
                      Originaly thought of using a 2d vector storing x and y pos of the "entities" (they arent visual, just position numbers) and updating them by looping trough the vector but as said probably not the best idea. Is a quadtree more efficient in updating all members positions and "collision"?

                      Quoting from the Wikipedia article:

                      A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are the two-dimensional analog of octrees and are most often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions.

                      A easy optimization for setting a quadrant or an entire 2d space as a single color is to remove all subtrees of that tree, and instead store the color as a single value. Instead of setting the value for 1.000.000.000 cells, store a single cell that says that all cells have the same value. Searching for a collision can stop when this optimized node is encountered.

                      A penalty for this method is the potential for more pointer indirection, and additional runtime memory allocation if nodes aren't preallocated.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      J 1 Reply Last reply 29 Jun 2024, 17:52
                      0
                      • J jeremy_k
                        29 Jun 2024, 15:53

                        @StudentScripter said in Console App how to populate a 10.000*10.000 Grid efficiently?:

                        @JonB @jeremy_k
                        I will have some "entities" that will be able to move in one direction at a time. Could a quadtree be there a good solution?
                        Originaly thought of using a 2d vector storing x and y pos of the "entities" (they arent visual, just position numbers) and updating them by looping trough the vector but as said probably not the best idea. Is a quadtree more efficient in updating all members positions and "collision"?

                        Quoting from the Wikipedia article:

                        A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are the two-dimensional analog of octrees and are most often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions.

                        A easy optimization for setting a quadrant or an entire 2d space as a single color is to remove all subtrees of that tree, and instead store the color as a single value. Instead of setting the value for 1.000.000.000 cells, store a single cell that says that all cells have the same value. Searching for a collision can stop when this optimized node is encountered.

                        A penalty for this method is the potential for more pointer indirection, and additional runtime memory allocation if nodes aren't preallocated.

                        J Offline
                        J Offline
                        JonB
                        wrote on 29 Jun 2024, 17:52 last edited by JonB
                        #14

                        @jeremy_k said in Console App how to populate a 10.000*10.000 Grid efficiently?:

                        A easy optimization for setting a quadrant or an entire 2d space as a single color is to remove all subtrees of that tree,

                        Absolutely. If that's the sort of thing you want to do. Doesn't seem to relate to toggling the state of 100,000 adjacent bits in 5 million.

                        it stores all squares that shall be changed in a vector.
                        The squares changed can be anywhere in this big map,
                        eventually reaching many millions of squares needed to be changed at onces.

                        If that is what you need to do, again I'm not sure I see any use of these quadtrees. But only you need the exact situation.

                        1 Reply Last reply
                        0

                        13/14

                        29 Jun 2024, 15:53

                        • Login

                        • Login or register to search.
                        13 out of 14
                        • First post
                          13/14
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved