[英]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. 运行了大约2-3秒。 I was recommended to rewrite it using
PrincipalSearcher
class: 建议使用
PrincipalSearcher
类重写它:
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? 为什么会有任何性能差异?
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. 我能想到的唯一可能的原因是
.FindByIdentity
必须检查多个属性是否匹配,因为您没有确切指定要查找的属性。
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? 这有多快?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.