Jump to content
  • Advertisement
Sign in to follow this  
coolblue

[.net] Why does this crash my PC?

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by coolblue
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.


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!