ForumsProgramming ForumC++ Question

4 6477
Somewhat49
offline
Somewhat49
1,607 posts
Nomad

Ok so I was just messing around with C++ and I coded a troll program that basically looks like it's doing a whole lot of work when I've put what I call stalling code in which looks like this:
while (a < 999999999) { ++a; }
while (d < 999999999){ ++d; }
while (d >= 10){ ++c; --d; }
while (c >= 999999999){ --d; --c; };
It's simple enough until I realized that for the third line if I keep it as d>=10 then it's not really modifying the integer c much (btw all those letters are integers) so I switched it to (d

  • 4 Replies
Somewhat49
offline
Somewhat49
1,607 posts
Nomad

I don't know how but it seems my OP got cut off so Ill try to retype it here.

It's simple enough until I realized that for the third line if I keep it as d>=10 then it's not really modifying the integer c much (btw all those letters are integers) so I switched it to (d

Somewhat49
offline
Somewhat49
1,607 posts
Nomad

Ok third try:

I don't know how but it seems my OP got cut off so Ill try to retype it here.

It's simple enough until I realized that for the third line if I keep it as d>=10 then it's not really modifying the integer c much (btw all those letters are integers) so I switched it to (d is less then or equal to 10) so that it would run that part of the code longer. The code without the modification takes 8 seconds to complete, the code with the modification still takes 8 seconds to complete even though it should be running longer with the modification.

So the question in can anyone tell me why that one switch isn't having any effect on the runtime of the stall?

Darkroot
offline
Darkroot
2,763 posts
Peasant

I'm not sure what you want or what you are stuck with but hopefully I can make your own code more clear to you including showing you how to test code so you can find out what's happening under the hood.

http://i.imgur.com/F3nMDn7.png

KentyBK
offline
KentyBK
566 posts
Nomad

A few things:

First off, if the point is just to stall, why not just loop infinitely instead of having a bunch of while loops like that?
Secondly, what're the initials values for your ints? Feel free to correct me if I'm wrong, but I'm going to assume they're all at 0.

Now onto your question:

So the question in can anyone tell me why that one switch isn't having any effect on the runtime of the stall?


It's not having any effect because you have it backwards. The code without the modification is actually the correct one if you want to stall.

After the first two while loops are done, both a and d are 999999999. Since d is also greater than 10, we go into the third loop. Of course, with the modification, the condition is no longer satisfied, because d is larger than 10 at that point.

You may also want to correct the forth loop, because we never enter it, both with and without the modification. After the third loop, c has a value of 999999990, because we stop incrimenting it as soon as d has a value of 9. Meaning c never actually gets large enough for the forth condition.

As for the run-time, I did a little testing myself. The unmodified code took ~9 seconds complete, while the modified code took a little over 5 seconds. The times being closer for you most likely depends on your system.
Showing 1-4 of 4