Speed up loops

I have a program with a while loop that is running really slowly. I'm making a program that finds how many tries it takes to reach a certain password but it takes about five minutes to get to the fourth character in the string and I'm hoping to get to around 18 characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while (cont == 1)
{
      cout << endl << time << " tries Password:  " << tries << endl;
      if (tries == password)
      {
                break;
      }
      L++;
      time++;
      tries = G;
      tries += L;
      if (L == 126)
      {
            G++;
            if (G == 126 && L == 126)
            {
                  G = 32;
                  L = 32;
                  cont++;
            }
            
            L = 32;
      }
}

These are just for the first two characters but the rest of the code so far is fairly similar. The CAP letters are characters. Please help.
Well, you can't expect much speed if you print a line in every iteration of the loop, so you should remove that.
Aside from that, I have no idea what the code is supposed to do, except that it can be improved significantly (that probably applies to execution speed as well). You should not use magic numbers (126, 32) or non-descriptive variable names (L, G).
Aside from that, are you compiling with -O3? -march=native can also help if the program is not supposed to run on other machines.
Last edited on
I'm programming with Dev-C++. I used 32 and 126 to narrow down the characters to ones that you can find on an English keyboard. The program in general is to type in a word and it tells you how many tries it takes to get the word. I chose simple variables to make it easier to remember instead of longer ones to keep track of. I was wondering if there was any way to make the while loop execute at a faster rate.
Sorry. I think I see what you mean by removing the lines but I'm not quite sure what you mean by that. Could you please explain just that part. I get the rest.
Move your cursor to somewhere in that line: cout << endl << time << " tries Password: " << tries << endl;
Then press End, Shift+Home, Backspace, Backspace.


I used 32 and 126 to narrow down the characters to ones that you can find on an English keyboard.

If you mean characters, you should use ' ' and '~'.

I chose simple variables to make it easier to remember instead of longer ones to keep track of.

Using non-obvious and non-descriptive variable names will just make it easier to remember when you look at the code later.
Just don't do it.
Last edited on
often, the slowest part of a program is I/O - that means to/from the screen, disk, socket, etc... so the more often you do it, the slower your program will run - writing to cout counts

when possible, you want to chunk I/O together and call them less often

using buffers is one way to chunk I/O
Thanks. That worked so much faster. I guess it must have been slow because it had to show all it's work as it went.
The variables are actually characters in my passwords. I chose them so I could eventually apply my password to the program and find out how secure it is. The variables hold the key to it which is why I chose them
Topic archived. No new replies allowed.