Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
Hi Friends,

There is some code snippet below, please suggest me which approach is better.


Approach One:

C#
STNList = new List<STNforGCN>();
               while (reader.Read())
               {
                   STNList.Add(LoadSTN(reader));
               }
               this.Connection.Close();



 private STNforGCN LoadSTN(IDataReader reader)
        {
            STNforGCN stn = new STNforGCN();
            stn.STNID = Convert.ToInt32(reader["STNID"]);
            stn.STNNumber = reader["STNNumber"].ToString();
            stn.GeneratedForEntityID = Convert.ToInt32(reader["GeneratedForEntityID"]);
            stn.Value = Math.Round(Convert.ToDecimal(reader["VALUE"]), 2);
            stn.BrandName = reader["BrandName"].ToString();
            stn.StnStatus = reader["Status"].ToString();
            return stn;
        }



Approach Two:
C#
 STNforGCN stn=null;
while (reader.Read())
               {
              
            stn = new STNforGCN();
            stn.STNID = Convert.ToInt32(reader["STNID"]);
            stn.STNNumber = reader["STNNumber"].ToString();
            stn.GeneratedForEntityID = Convert.ToInt32(reader["GeneratedForEntityID"]);
            stn.Value = Math.Round(Convert.ToDecimal(reader["VALUE"]), 2);
            stn.BrandName = reader["BrandName"].ToString();
            stn.StnStatus = reader["Status"].ToString();
             STNList.Add(stn);
               }
               this.Connection.Close();
Posted
Updated 11-Jul-13 20:34pm
v2
Comments
Richard MacCutchan 12-Jul-13 3:42am    
Better for what?
Deenuji 12-Jul-13 3:47am    
elaborate your question????
maneavnash 12-Jul-13 3:47am    
Approach One is better ....
shreeniwas kushwah 12-Jul-13 3:58am    
If i use private method LoadSTN() then if list has 10000000 records then 10000000 objects of STN class created and if i use Approch two then single object initialize memory 10000000 times

then what is better approch????
shreeniwas kushwah 12-Jul-13 4:37am    
can any one help me????

1 solution

Approach One is likely to be considered "better" by people. The reason for this is that its design is better with the creation of a STNforGCN object separated from iterating over the result list.

As for performance, they're about the same for all practical scenarios. You will have to pay the overhead of a method call per line in the result with approach one, but that is likely to be insignificant compared to the rest of the work carried out.

You are in both approaches allocating and initializing the same number of STNforGCN objects, the fact that Approach two re-uses a local variable saves at most moving the stack-pointer which is going to be very fast anyway.

If you're really concerned about performance you should use a profiler instead of guessing, modern compilers can do awesome things to your code so it is very difficult to determine absolute performance differences by just looking at the code. For example, if the compiler decides to inline LoadSTN in Approach One, then the solutions are pretty much identical.

Hope this helps,
Fredrik
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900