Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I speed up my code?

How can I speed up my code?

Scheduled Pinned Locked Moved C#
questioncomperformance
9 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }

    M D 2 Replies Last reply
    0
    • T turbosupramk3

      I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      By domain, you mean AppDomain? If so, performance will always be terrible because it has to serialize everything when crossing a domain. If not, then ignore this answer. Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

      1 Reply Last reply
      0
      • T turbosupramk3

        I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        This doesn't make a lot of sense. Are you getting all of the group memberships of a specified user or are you getting all of the group memberships of a list of users who are members of a specified group, or what? You never spelled out exactly what this code is supposed to be doing.

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        T 1 Reply Last reply
        0
        • D Dave Kreskowiak

          This doesn't make a lot of sense. Are you getting all of the group memberships of a specified user or are you getting all of the group memberships of a list of users who are members of a specified group, or what? You never spelled out exactly what this code is supposed to be doing.

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          T Offline
          T Offline
          turbosupramk3
          wrote on last edited by
          #4

          My goal is to enumerate through the groups and subgroups and get all of the non duplicate groups/subgroups and put them into a list. After that I checked to see if a specific user is a member of any of the groups I've enumerated to the list of groups/subgroups.

          D 1 Reply Last reply
          0
          • T turbosupramk3

            My goal is to enumerate through the groups and subgroups and get all of the non duplicate groups/subgroups and put them into a list. After that I checked to see if a specific user is a member of any of the groups I've enumerated to the list of groups/subgroups.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            "Enumerate through the groups and subgroups" of what? What is the starting object for this query? Are you returning ALL of the groups in the directory? There is no such thing as a "subgroup". What I think you're referring to is a group that is a member of another group. Or why not just start with the user object and get the list of groups it is a member of?

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            T 1 Reply Last reply
            0
            • D Dave Kreskowiak

              "Enumerate through the groups and subgroups" of what? What is the starting object for this query? Are you returning ALL of the groups in the directory? There is no such thing as a "subgroup". What I think you're referring to is a group that is a member of another group. Or why not just start with the user object and get the list of groups it is a member of?

              A guide to posting questions on CodeProject

              Click this: Asking questions is a skill. Seriously, do it.
              Dave Kreskowiak

              T Offline
              T Offline
              turbosupramk3
              wrote on last edited by
              #6

              I am enumerating through groups recursively. There is a starting parent group, and I'm starting with the parent and enumerating through all of the child groups of that parent and every group that is a member of the child groups that are found. At my organization, there may be a nested hierarchy of 12 groups or more and unfortunately there may be some of the same groups nested in other groups as the local admin qualification is to have a pulse. Does that explain it better?

              D 1 Reply Last reply
              0
              • T turbosupramk3

                I am enumerating through groups recursively. There is a starting parent group, and I'm starting with the parent and enumerating through all of the child groups of that parent and every group that is a member of the child groups that are found. At my organization, there may be a nested hierarchy of 12 groups or more and unfortunately there may be some of the same groups nested in other groups as the local admin qualification is to have a pulse. Does that explain it better?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                That's better. If you are making a request to the domain controller for every group at every level, you're going through an expensive operation, every single time. Just because you're code hits one domain controller for one request does not mean that the same domain controller is going to service all subsequent requests. You could be throwing requests all over the network without knowing it. Now, each request is expensive so it would be better to make them once and then cache the result. Create a Dictionary to cache the objects returned. When you make a request for a group, lookup the group name in the cache to see if it's there first. If not, go to the directory to get it and cache it for use next time around. Whenever you go get a list of groups, add them to the cache if needed.

                A guide to posting questions on CodeProject

                Click this: Asking questions is a skill. Seriously, do it.
                Dave Kreskowiak

                T 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  That's better. If you are making a request to the domain controller for every group at every level, you're going through an expensive operation, every single time. Just because you're code hits one domain controller for one request does not mean that the same domain controller is going to service all subsequent requests. You could be throwing requests all over the network without knowing it. Now, each request is expensive so it would be better to make them once and then cache the result. Create a Dictionary to cache the objects returned. When you make a request for a group, lookup the group name in the cache to see if it's there first. If not, go to the directory to get it and cache it for use next time around. Whenever you go get a list of groups, add them to the cache if needed.

                  A guide to posting questions on CodeProject

                  Click this: Asking questions is a skill. Seriously, do it.
                  Dave Kreskowiak

                  T Offline
                  T Offline
                  turbosupramk3
                  wrote on last edited by
                  #8

                  I have not created a dictionary before, so do you recommend this be stored in a text file or in a database? This morning I got 323 group results in 65 seconds, I would imagine that when I try after lunch it will take 10x as long. Is it better for me to try and dictate which DC I am hitting? I can view which DC I'm hitting, so maybe my issue of sporadicness is actually a DC problem? What do you think?

                  D 1 Reply Last reply
                  0
                  • T turbosupramk3

                    I have not created a dictionary before, so do you recommend this be stored in a text file or in a database? This morning I got 323 group results in 65 seconds, I would imagine that when I try after lunch it will take 10x as long. Is it better for me to try and dictate which DC I am hitting? I can view which DC I'm hitting, so maybe my issue of sporadicness is actually a DC problem? What do you think?

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Dictionary Class[^] You don't store it anywhere.

                    A guide to posting questions on CodeProject

                    Click this: Asking questions is a skill. Seriously, do it.
                    Dave Kreskowiak

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups