Click here to Skip to main content
15,921,210 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
this code is repeated for all columns of DGV. I think defining a lot of variables will affect performance.notice I need "x" variables because somewhere in code I use them for calculating.

C#
TimeSpan  x1 = TimeSpan.Zero;
string s1 = dataGridView1.Rows[i].Cells["MrnngEnt"].Value.ToString();
 if (!string.IsNullOrEmpty(s1))
    x1 = TimeSpan.Parse(s1= (s1 == "24:00:00") ? "23:59:59" : s1);
Posted
Updated 3-Mar-14 1:38am
v2
Comments
Vedat Ozan Oner 3-Mar-14 7:48am    
I couldn't understand what your problem is exactly.
Maciej Los 3-Mar-14 7:52am    
COuld you be more specific and provide more details about yoour issue?
mit62 3-Mar-14 8:05am    
I don't want declare variable s1 because I think declare a lot of variables will take place memory and hit performance.is there any way to avoid declaring s1 variable?

Well you could get all of
C#
if (!string.IsNullOrEmpty(s1))
    x1 = TimeSpan.Parse(s1= (s1 == "24:00:00") ? "23:59:59" : s1);

as a single line using the ternary operator again.
x1 = (!string.IsNullOrEmpty(s1)) ? TimeSpan.Parse(s1 = (s1 == "24:00:00") ? "23:59:59" : s1) : TimeSpan.Zero


But personally I would write a little helper function to do that data adjustment for you. E.g.
C#
private TimeSpan AdjustTime(int row, string colName)
{
    string s1 = dataGridView1.Rows[row].Cells[colName].Value.ToString();
    return ((!string.IsNullOrEmpty(s1)) ? TimeSpan.Parse(s1 = (s1 == "24:00:00") ? "23:59:59" : s1) : TimeSpan.Zero);
}

Then you would use it like this
TimeSpan x1 = AdjustTime(i, "MrnngEnt");
TimeSpan x2 = AdjustTime(i, "NextColumn");
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 8:11am    
Very good hint, +5!
I think making such sub-optimizations (such reducing the number of variables) does not make sense, because, probably:
  • You don't need to optimize (are you sure this part of your application is the bottleneck?)
  • Performance is not affected by the 'number of variables'.
  • The compiler is smarter than us and could optimize such code more effectively


Unless you are really sure a piece of code needs to be optimized (have you rpofiling results?), don't try to do it. A cleaner code more valuable.
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 8:11am    
Valuable advice, +5!
CPallini 3-Mar-14 10:47am    
Thank you.
midnight_ 3-Mar-14 8:14am    
+1 for question the sense

If you optimize code that way, then its a matter of readability.

Using a ternary op or IF doesn't count at all, since the compiler interpret it the same.
And that will absolutly not gain any kind of performance.
CPallini 3-Mar-14 10:47am    
Thank you.
Sergey Alexandrovich Kryukov 3-Mar-14 10:37am    
Great points, a 5. Way too many beginners are driven by big misconceptions on the "optimal" code, and on the value of code in general.
—SA

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