简体   繁体   English

为什么使用PrincipalSearcher比FindByIdentity()更快?

[英]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.

相关问题 在这个例子中,为什么使用比列表更快的元组? - Why is using a Tuple faster than a List in this example? 为什么 AddRange 比使用 foreach 循环更快? - Why is AddRange faster than using a foreach loop? 使用goto语句会比让if语句返回false更快吗? - Would using a goto statement be faster than letting an if statement return false? 为什么ReferenceEquals()比==更快 - Why ReferenceEquals() is faster than == 使用PrincipalSearcher查找具有“或”参数的用户 - Using PrincipalSearcher to find users with “or” parameters 为什么SqlQuery比在视图上使用LINQ表达式快得多? - Why is SqlQuery a lot faster than using LINQ expression on views? 为什么在Image上旋转比使用BitmapEncoder快得多? - Why is rotation much faster on Image than using BitmapEncoder? 为什么复制流然后使用BinaryFormatter反序列化比仅反序列化更快 - Why is copying a stream and then deserializing using a BinaryFormatter faster than just deserializing 为什么创建和使用表达式比直接访问更快? - Why is creating and using an Expression faster than direct access? 在这种情况下,AddRange()会比ToList()快吗? - Would AddRange() be faster than ToList() in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM