|Win 8/64 Visual Studio 2012 v.3, WinForms C#, FrameWork 4.5: no use of explicit threading in the project.
I've come to rely on using 'Stopwatch for a timer, and I am surprised to find, in a current project, a huge difference between what I observe, and what Stopwatch reports.
I have a very complex generic class, whose generic Types include other generic Type classes. I am able to create any number of instances of the outer class without a problem, and verify their contents.
Using Stopwatch to time the creation of the instances seems to give results consistent with what I observe. I'm not bothering, in this case, to do a "dry run" to pre-JIT.
Where what I observe really varies from what Stopwatch returns is: in the case where I loop through all the created instances in the outer class, and fill a StringBuilder with a complete kind of "report" on all the instances of the outer class, and all the various instances of the inner classes.
The inner classes have public Guid properties, set when their constructor is called (via 'private set).
If I create 10,000 instances of the outer class, and each of (in this case #2) of its inner classes have 10 objects each initialized as they are created. I end up with potentially 20k Guids, 100k each of the inner class contents. So, the resulting "report" (adding in tab characters, and other formatting) will come out to about 250k lines of text, containing about 4mb of characters.
From pressing the button that generates the "report" to seeing the report-TextBox filled, takes around thirty seconds, but Stopwatch is reporting the operation took 300~800 milliseconds.
1. there's something about using Stopwatch in the scope of a Button_Click EventHandler that leads to errors. Threading issue ? Doubtful, imho, since object creation seems to time okay.
2. there's something about using StringBuilder, and then converting to 'string, and setting the 'Text property of a TextBox that compromises Stopwatch. Wrapping TextBox refresh in 'SuspendLayout and 'ResumeLayout seem to have no appreciable effect.
Here's the code as stripped-down as possible to indicate how the report is being generated, and Stopwatch is being used:
for (int i = 0; i < KVPList.Count; i++)
tbReport.Text = sb.ToString();
tbEventReport.Text = "Report Displayed: Time(ms): " + watch2.Elapsed.MillisecondsAppreciate any suggestions, or ideas; very high-precision timing is not really needed here.
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal