   [.net] Why does this crash my PC?
  I am testing some things to increase performance and have written this very basic application to time the different performance tests. This current test ti designed to test the difference between checking for an empty string with #string#==string.Empty, against #string#.Length>0.However either the first run or successive runs cause my machine to switch itself off!! I have had some problems in the past with some unstable memory (although I thought this was now fixed).  I would just like to gather views on whether it is the code or the hardware which is at fault.the code is as follows:-private void button1_Click(object sender, System.EventArgs e){  label1.Visible=false;  string s1="qwertyuiop";  string s2=string.Empty;  int x=0;  DateTime ww=DateTime.Now;  for(double i=0;i<=100000000;i++)  {    if(s1.Length>0)    { x=0;    }  }  DateTime we=DateTime.Now;  TimeSpan ss=we-ww;  string sss=ss.Milliseconds.ToString();  textBox1.Text=sss;  label1.Visible=true;}   
Quote:for(double i=0;i<=100000000;i++){if(s1.Length>0){x=0;}}Why are you doing a loop with a double? In fact why is there a loop here at all? Its doing nothing... s1.Length is not changing therefore it will always be above 0 therefore x = 0 and you can quit. Why woud you iterate 100000000 times? I just don't see the purpose of this code at all.   ;  The loop is there to iterate through a few thousand times to get a time difference. Just doing one if statement is so quick that it is does not register. If I test large loops I get a measurable result. I was using an Int but as it was crashing I thought that perhaps I was exceeding the size constraints so I changed it to a double. (I have since discovered that an Int can hold a greater number than this).   ;  I've seen code like that blow up on overclocked PCs or PCs with bad memory. The code itself doesn't look like it should crash anything.   ;  Ah, isn't there a sleep method of some sort? There should be, I would definitely change it to that method, also have you tried stepping it in a debugger?[EDIT]Found it:Sleep MethodAlso Take A Look At This:http://www.codersource.net/csharp_measure_execution_time.aspx;  I will look into using a sleep method. I have not stepped through as it sometimes runs fine and other times it will cause my PC to turn off.   ;  GDKnight, thank you for the example. I am doing much the same thing but it did highlight the fact that I was not returning the total elapsed milliseconds. The problem with using sleep is that the "if" statement will still not register an elapsed time. If I call sleep and set it to 10 ms the "if" statement runs so quickly that the elapsed time is 10 ms. I think I am stuck with the loop method.   ;Quote:Original post by coolblueGDKnight, thank you for the example. I am doing much the same thing but it did highlight the fact that I was not returning the total elapsed milliseconds. The problem with using sleep is that the "if" statement will still not register an elapsed time. If I call sleep and set it to 10 ms the "if" statement runs so quickly that the elapsed time is 10 ms. I think I am stuck with the loop method.You could try calulating the first 30 digits of pi, something with calculations.... At least then it might be such a long loop, just a thought though. Are you overclocking your pc though, that might just save trouble if you are. I have never seen that but if SiCrane has.   ;  Ok, using totalmilliseconds, I get a meaningful result from a slightly smaller loop which does not appear (so far) to be crashing my pc. Also for your information, testing for an empty string using if(str==String.Empty) takes around 4 times longer than testing with if(str.Length==0).GDKnight, I am not overclocking my PC, but it has had memory instability problems in the past.Thanks for your help   ;  Just to be safe, I'd seriously suggest running memtest86.   