Click here to Skip to main content
14,383,854 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have usersIds list for each one has 200 above secuirty roles.
I have userd the below code to execute but taking lot of time.
Could you please give the suggestion that what is the best approach for this type of scenarios in C#

List<SRInformation> objSrInformation = new List<SRInformation>();
//UserList 250
foreach(int userId in userList)
{
      List<SRInformation> SRInformationlist = new List<SRInformation>();
      List<int> srList =new List<int>();
      srList = GetSRListByUserID(id);
      //SR List each one conatin min 200
      foreach(int srId in srList)
      {
	SRInformationlist = GetDetailsBySRId(srID)
        objSrInformation.Add(SRInformationlist);
      }
      
}


What I have tried:

I have tried
List<SRInformation> objSrInformation = new List<SRInformation>();
//UserList 250
foreach(int userId in userList)
{
      List<SRInformation> SRInformationlist = new List<SRInformation>();
      List<int> srList =new List<int>();
      srList = GetSRListByUserID(id);
      //SR List each one conatin min 200
      foreach(int srId in srList)
      {
	SRInformationlist = GetDetailsBySRId(srID)
        objSrInformation.Add(SRInformationlist);
      }
      
}
Posted
Updated 16-Jul-19 5:45am
Comments
Richard Deeming 16-Jul-19 13:45pm
   
objSrInformation.Add(SRInformationlist);

That line won't compile. Did you mean AddRange?

List<int> srList = new List<int>();
srList = GetSRListByUserID(id);

Why create a new object if you're just going to throw it away without using it?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

We can't tell, we have no idea where it is slow - let alone why.
Start by profiling your app and finding out where it's taking time, and how much it's actually taking. Then you can start f]think about ways to improve it, and have a benchmark for how big an improvement you are getting. Manual profiling would involve adding Stopwatch class[^] instances to your code, and logging the time various bits take, or you can use the Visual Studio profiler[^] if your version of VS supports it.

Chances are that multithreading won't help significantly unless it's targeted at parallelable bottlenecks - it's not a magic bullet, it involves adding threads (which add processing time to support) each of which needs a free core to execute. Once all the cores in your machine are in use, thread get suspended waiting for a free one and do not run until one is available. Adding threads indiscriminately can slow applications significantly as each thread uses more memory and requires extra processing power to manage and switch the threads.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100