Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:12:42+00:00 2026-05-11T14:12:42+00:00

I have a very simple function which takes in a matching bitfield, a grid,

  • 0

I have a very simple function which takes in a matching bitfield, a grid, and a square. It used to use a delegate but I did a lot of recoding and ended up with a bitfield & operation to avoid the delegate while still being able to perform matching within reason. Basically, the challenge is to find all contiguous elements within a grid which match the match bitfield, starting from a specific ‘leader’ square. Square is somewhat small (but not tiny) class. Any tips on how to push this to be even faster? Note that the grid itself is pretty small (500 elements in this test).

Edit: It’s worth noting that this function is called over 200,000 times per second. In truth, in the long run my goal will be to call it less often, but that’s really tough, considering that my end goal is to make the grouping system be handled with scripts rather than being hardcoded. That said, this function is always going to be called more than any other function.

Edit: To clarify, the function does not check if leader matches the bitfield, by design. The intention is that the leader is not required to match the bitfield (though in some cases it will).

Things tried unsuccessfully:

  • Initializing the dictionary and stack with a capacity.
  • Casting the int to an enum to avoid a cast.
  • Moving the dictionary and stack outside the function and clearing them each time they are needed. This makes things slower!

Things tried successfully:

  • Writing a hashcode function instead of using the default: Hashcodes are precomputed and are equal to x + y * parent.Width. Thanks for the reminder, Jim Mischel.
  • mquander’s Technique: See GetGroupMquander below.
  • Further Optimization: Once I switched to HashSets, I got rid of the Contains test and replaced it with an Add test. Both Contains and Add are forced to seek a key, so just checking if an add succeeds is more efficient than adding if a Contains fails check fails. That is, if (RetVal.Add(s)) curStack.Push(s);

    public static List<Square> GetGroup(int match, Model grid, Square leader) {     Stack<Square> curStack = new Stack<Square>();     Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();     curStack.Push(leader);     while (curStack.Count != 0)     {         Square curItem = curStack.Pop();         if (Retval.ContainsKey(curItem)) continue;         Retval.Add(curItem, true);         foreach (Square s in curItem.Neighbors)         {             if (0 != ((int)(s.RoomType) & match))             {                 curStack.Push(s);             }         }     }     return new List<Square>(Retval.Keys); } 

=====

    public static List<Square> GetGroupMquander(int match, Model grid, Square leader)     {         Stack<Square> curStack = new Stack<Square>();         Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();         Retval.Add(leader, true);         curStack.Push(leader);         while (curStack.Count != 0)         {             Square curItem = curStack.Pop();              foreach (Square s in curItem.Neighbors)             {                 if (0 != ((int)(s.RoomType) & match))                 {                     if (!Retval.ContainsKey(s))                     {                         curStack.Push(s);                         Retval.Add(curItem, true);                     }                 }             }         }         return new List<Square>(Retval.Keys);     } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:12:42+00:00Added an answer on May 11, 2026 at 2:12 pm

    The code you posted assumes that the leader square matches the bitfield. Is that by design?

    I assume your Square class has implemented a GetHashCode method that’s quick and provides a good distribution.

    You did say micro-optimization . . .

    If you have a good idea how many items you’re expecting, you’ll save a little bit of time by pre-allocating the dictionary. That is, if you know you won’t have more than 100 items that match, you can write:

    Dictionary<Square, bool> Retval = new Dictionary<Square, bool>(100); 

    That will avoid having to grow the dictionary and re-hash everything. You can also do the same thing with your stack: pre-allocate it to some reasonable maximum size to avoid resizing later.

    Since you say that the grid is pretty small it seems reasonable to just allocate the stack and the dictionary to the grid size, if that’s easy to determine. You’re only talking grid_size references each, so memory isn’t a concern unless your grid becomes very large.

    Adding a check to see if an item is in the dictionary before you do the push might speed it up a little. It depends on the relative speed of a dictionary lookup as opposed to the overhead of having a duplicate item in the stack. Might be worth it to give this a try, although I’d be surprised if it made a big difference.

    if (0 != ((int)(s.RoomType) & match)) {     if (!Retval.ContainsKey(curItem))         curStack.Push(s); } 

    I’m really stretching on this last one. You have that cast in your inner loop. I know that the C# compiler sometimes generates a surprising amount of code for a seemingly simple cast, and I don’t know if that gets optimized away by the JIT compiler. You could remove that cast from your inner loop by creating a local variable of the enum type and assigning it the value of match:

    RoomEnumType matchType = (RoomEnumType)match; 

    Then your inner loop comparison becomes:

    if (0 != (s.RoomType & matchType)) 

    No cast, which might shave some cycles.

    Edit: Micro-optimization aside, you’ll probably get better performance by modifying your algorithm slightly to avoid processing any item more than once. As it stands, items that do match can end up in the stack multiple times, and items that don’t match can be processed multiple times. Since you’re already using a dictionary to keep track of items that do match, you can keep track of the non-matching items by giving them a value of false. Then at the end you simply create a List of those items that have a true value.

        public static List<Square> GetGroup(int match, Model grid, Square leader)     {         Stack<Square> curStack = new Stack<Square>();          Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();          curStack.Push(leader);         Retval.Add(leader, true);         int numMatch = 1;         while (curStack.Count != 0)         {             Square curItem = curStack.Pop();              foreach (Square s in curItem.Neighbors)              {                 if (Retval.ContainsKey(curItem))                     continue;                 if (0 != ((int)(s.RoomType) & match))                 {                     curStack.Push(s);                     Retval.Add(s, true);                     ++numMatch;                 }                 else                 {                     Retval.Add(s, false);                 }             }         }         // LINQ makes this easier, but since you're using .NET 2.0...         List<Square> matches = new List<Square>(numMatch);         foreach (KeyValuePair<Square, bool> kvp in Retval)         {             if (kvp.Value == true)             {                 matches.Add(kvp.Key);             }         }         return matches;     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 224k
  • Answers 224k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RealVector.append() doesn't modify the vector, but rather constructs a new… May 13, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer I believe what you're looking for is URI::Split, e.g.: use… May 13, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer When you see the method in View >> Object Browser… May 13, 2026 at 12:48 am

Related Questions

From what I've seen in the past, StackOverflow seems to like programming challenges, such
I have a list of files, and i need to read them each in
I'm not an C++ expert and still do not have a great intuitive grasp
I have an application in which most requests are submitted via AJAX, though some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.