Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would using PrincipalSearcher be faster than FindByIdentity()?

I had this code:

var context = new PrincipalContext( ContextType.Machine );
var user = UserPrincipal.FindByIdentity( context, username );

and it took about 2-3 seconds to run. I was recommended to rewrite it using PrincipalSearcher class:

var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;

and it runs in less than one second - notably faster. The person why advised the rewrite is as clueless as me why it runs faster.

Why does it make any performance difference?

like image 459
sharptooth Avatar asked Aug 03 '12 16:08

sharptooth


1 Answers

The only plausible reason I can think of is that .FindByIdentity has to check multiple attributes for a match, since you're not specifying exactly which attribute you're looking for.

You can do that by specifying the attribute you're looking for (using this method overload) - try this for a comparison:

var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

How fast is this?

like image 51
marc_s Avatar answered Oct 29 '22 13:10

marc_s