Search code examples
c#asp.net-membershipwebmethodmembership-provider

How to reduce the execution time? ProfileBase's GetPropertyValue takes 40ms to execute


I have following code, wherein I create list of a custom class using MembershipUser array.

Following is the custom class whose list is created:

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;
    }

}

Following is the code which populates userlist:

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

Following is the code which generates list of userandGroup (the custom class):

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);
    }

As written in the comment, the line;

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

takes 40ms to complete in one loop. I have 40 users at the moment. Thus the loop takes approximately 1600ms to execute. If number of users are increased, the loop will take horrendous time to complete.

How can I reduce the execution time of the line or is there any other way to get first name and last name of the user from ProfileBase?


Solution

  • As per @TyCobb 's suggestion, I used Parallel Foreach loop. I updated the loop as follows.

    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);
            }
        });
    

    Although this improved performance a bit, the performance is yet not optimum. Now the entire loop completes in less than one second.