Why would using PrincipalSearcher be faster than FindByIdentity()?

Viewed 5862

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?

1 Answers
Related