You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server 2008 database. The application contains two SqlCommand objects named cmd1 and cmd2.
You need to measure the time required to execute each command. Which code segment should you use?
A.
Stopwatch w1 = new Stopwatch();
w1.Start();
cmd1.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
w1.Start();
cmd2.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
B.
Stopwatch w1 = new Stopwatch();
w1.Start();
cmd1.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
w1.Reset();
cmd2.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
C.
Stopwatch w1 = Stopwatch.StartNew();
cmd1.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
w1.Start();
cmd2.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
D.
Stopwatch w1 = Stopwatch.StartNew();
cmd1.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
w1 = Stopwatch.StartNew();
cmd2.ExecuteNonQuery();
w1.Stop();
Trace.WriteLine(w1.ElapsedMilliseconds);
Explanation:
A & C do not reset the stopwatch before running cmd2. B does not start the stopwatch after resetting the stopwatch
Start() does not reset the stopwatch, whereas StartNew() will create a new instance of the Stop watch and initialise the elapsed time to Zero.Stopwatch Class
(http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx)