【问题标题】:How to reduce the execution time? ProfileBase's GetPropertyValue takes 40ms to execute如何减少执行时间? ProfileBase 的 GetPropertyValue 执行需要 40ms
【发布时间】:2018-06-11 21:33:16
【问题描述】:

我有以下代码,其中我使用MembershipUser 数组创建自定义类的列表。

以下是创建列表的自定义类:

public class userandGroup :IComparable{
    public string id { get; set; }
    public string Name { get; set; }
    public string DisplayName { get; set; }
    public string type { get; set; }
    public int? CompareTo(Object obj)
    {
        if (obj is userandGroup)
            return this.DisplayName.CompareTo((obj as userandGroup).DisplayName);
        return null;
    }

}

以下是填充userlist的代码:

MembershipUserCollection tempuserlist = GetProvider("DefaultProfileProvider", applicationName).GetAllUsers(currentPage - 1, pageSize, out totalUsers);
MembershipUser[] userlist = new MembershipUser[totalUsers];
tempuserlist.CopyTo(userlist, 0);

以下是生成userandGroup(自定义类)列表的代码:

foreach (MembershipUser usr in userlist)
    {
        userandGroup usrgp = new userandGroup();
        usrgp.id = ((Guid)usr.ProviderUserKey).ToString() ;
        usrgp.Name = usr.UserName;
        ProfileBase profile = ProfileBase.Create(usr.UserName);
        profile.Initialize(usr.UserName, true);
        // Following line approximately takes 40ms per loop.
        usrgp.DisplayName = profile.GetPropertyValue("FirstName").ToString() + " " + profile.GetPropertyValue("LastName").ToString();
        usrgp.type = "user";
        lst.Add(usrgp);
    }

正如评论中所写,该行;

usrgp.DisplayName = profile.GetPropertyValue("FirstName").ToString() + " " + profile.GetPropertyValue("LastName").ToString(); 

一个循环需要 40 毫秒才能完成。我目前有 40 个用户。因此,循环执行大约需要 1600 毫秒。如果用户数量增加,循环将花费大量时间来完成。

如何减少行的执行时间,或者有没有其他方法可以从ProfileBase 获取用户的名字和姓氏?

【问题讨论】:

  • 如果你觉得你已经足够优化调用并且不关心lst的顺序,你总是可以考虑并行处理你的用户。
  • @TyCobb 怎么做?代码位于同步调用的 Web 服务中。
  • 它会阻塞,但可以同时运行您收藏中的许多项目。 msdn.microsoft.com/en-us/library/dd992001(v=vs.110).aspx
  • @TyCobb,谢谢。那确实奏效了。循环执行时间现在减少到大约 750 毫秒。但我敢肯定,循环的执行时间不应超过 100 毫秒。对于 40 个用户,现在大约需要。 750 毫秒,如果我有 1000 个或更多用户怎么办?
  • 如果对象不是userandGroup(或者是null),你真的希望CompareTo返回0吗?这意味着他们是平等的......

标签: c# asp.net-membership webmethod membership-provider


【解决方案1】:

根据 @TyCobb 的建议,我使用了 Parallel Foreach 循环。我将循环更新如下。

Object obj = new Object();
    Parallel.ForEach(userlist, (usr) =>
    {
        userandGroup usrgp = new userandGroup();
        usrgp.id = ((Guid)usr.ProviderUserKey).ToString();
        usrgp.Name = usr.UserName;
        ProfileBase profile = ProfileBase.Create(usr.UserName);
        profile.Initialize(usr.UserName, true);
        usrgp.type = "user";
        usrgp.DisplayName = profile.GetPropertyValue("FirstName").ToString() + " " + profile.GetPropertyValue("LastName").ToString();
        lock (obj)
        {                
            lst.Add(usrgp);
        }
    });

虽然这稍微提高了性能,但性能还不是最佳的。现在整个循环不到一秒就完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多