Author Topic: C++ Programmin Quick Help  (Read 965 times)

I have a while loop with no delay, however if I put a cout in there it slows it down, alot.


How do I cout in a while loop without slowing it?

You added something to your program, meaning it now has less time to do other things. There's no way to "fix" it.

cout shouldn't slow down the loop "a lot" unless there's nothing else in the loop.

You can instead try something like this..

Code: [Select]
#include <stdio.h>
char* str[10];
for(int i = 0; ; i++) {
    if(i > 9) {
        printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7], str[8], str[9]);
        i = 0;
    }
    str[i] = "whatever you're printing";
}

This way it only prints every 10 loops, but regardless it shouldn't be slowing down your loop by that much that you need a 'faster' way. I/O is inherently expensive. Don't use it when you don't have to.

Also, this actually wouldn't speed up your process any (it'd actually slow it down from extra calculations) but it would increase the speed between your calculations.

This is my current code:
Code: [Select]
while (z = z)
{
x++;
y++;

if (x == xx)
x = (x - xx) * (-1);

if (y == yy)
y = (y - yy) * (-1);

MoveWindow(window,x,y,100,100,1);
Sleep(0);
}
The sleep is for troubleshooting reasons.
When this loop is ran the window appears all over the screen as it happens too fast for the eye to actually follow.

However if I add a cout line in there, it slows down so much that the window only moves every 2 seconds.

http://stackoverflow.com/questions/14573424/c-does-cout-statement-makes-code-slower
from what i'm getting from this thread, you should flush the buffer after every line.

Well, first off, = is not a boolean operator.

What you meant to do was while(z == z), but which may as well be while(true)

Also, moving the window is going to be a very expensive operation. You should probably just put that right before the loop.

One last thing, I don't see a cout.

i'm not a c++ guy, but in my experience, assuming "cout" does what I think it does (writes to console window/stdout?) having a lot of those will slow your program down tremendously.
the simple solution is to make it run less often, like trinick suggested.

Well, first off, = is not a boolean operator.

What you meant to do was while(z == z), but which may as well be while(true)

Also, moving the window is going to be a very expensive operation. You should probably just put that right before the loop.

One last thing, I don't see a cout.

It works either way for my purposes.

Moving the window is the whole reason for the while loop. and it certainly does not seem nearly as expensive as cout.

I wanted to point out what was in the while loop while it works as intended.


i'm not a c++ guy, but in my experience, assuming "cout" does what I think it does (writes to console window/stdout?) having a lot of those will slow your program down tremendously.
the simple solution is to make it run less often, like trinick suggested.

I only want one cout line.

Then put it before the while loop? If the cout is only being ran once, I don't think it's going to slow the rest of the code..