1

We are working on two different libraries which tends to perform the same work. I have been assigned to check which library performs the same work faster with given set of inputs. there are multiple test cases to determine this and i want to check how much time(in miliseconds) is taken by the library to perform the task.

Following is the outline of the code.

// preparation of input parameters
// Get the timestamp
// Calling the desired library with the set of input parameters
// again get the timestamp
// Process the output received from the library

// get a diff between timestamp and write it into a file.

I am supposed to do it in C#, and on reading up some provided docs and some online search i came up with following code which can be used to measure the time taken(in miliseconds)

int s1 = DateTime.Now.Millisecond;
int e1 = DateTime.Now.Millisecond;
int f1 = e1 - s1;
FileStream fs = new FileStream("Time_Diff.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
string str = Convert.ToString(f1);
sw.WriteLine(str);
sw.WriteLine(e1 + " " + s1);
sw.Flush();
sw.Close();

I just wanted to know that I am going in correct direction. Also if anybody can suggest me another way of doing the same thing?

another thing i wanted to know is what are other practical approches I can use to make sure that the library we are choosing will give the best performance?

another small request. Can somebody please let me know how to implement stopwatch(or any other similar way to get the timestamp)do the same thing in C++?

Thanks, Azeem

  • 1
    diff between `DateTime`-instances is not a good idea, as the precision is not that good (http://jaychapman.blogspot.co.at/2007/11/datetimenow-precision.html) ... use a `Stopwatch`-instance! anyway just use the keyword "benchmark" and "c#" for a sophisticated google-search ... or, just read up the following question here on SO: http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance –  Sep 03 '14 at 06:21
  • 3
    Use [`Stopwatch`](http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx) class instead of `DateTime`. [Related question](http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance). – Yurii Sep 03 '14 at 06:22
  • How long does each call take? You should make sure you're timing significant amounts of work - which may mean making the call thousands or millions of time. – Jon Skeet Sep 03 '14 at 06:22
  • Oh, and use `File.AppendAllText` - or if you *must* use a `Stream` and a `StreamWriter`, use `using` statements :) – Jon Skeet Sep 03 '14 at 06:23
  • Thanks for the help guys!!! Going with stopwatch implementation. – AzeemSheikh Sep 03 '14 at 06:38

1 Answers1

3

use stopwatch as follows.

        var stopWatch = System.Diagnostics.Stopwatch.StartNew();
        FileStream fs = new FileStream("Time_Diff.txt", FileMode.Append);
        StreamWriter sw = new StreamWriter(fs);
        //Total time required
        sw.WriteLine("\nTotal time: " + stopWatch.Elapsed.TotalSeconds.ToString());
        sw.Flush();
        sw.Close();
captainsac
  • 2,484
  • 3
  • 27
  • 48