How to search faster in c++?

Here is my code :

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
for(int n = 0;n <= 500000;n+=4) // from 0 to 500000 4 bytes
{
system("cls");
cout << n << hex << endl; // print memory addres of n(int)
}
system("cls");
cout << "finish" << endl;
system("pause");
return 0;
}

When run program wait a long time to finish
Last edited on
Not sure what you want the program to do.

You could make the program output "finish" much quicker by simply removing the loop.

I wont to make memory address Search(like Cheat Engine) this is not full code but this take Long time to finish
Last edited on
It will be faster when you remove system("cls"); inside the for loop.
Ty,maybe more way :D
It's slow because it's printing to the screen

Remove these lines and it will finish very fast.
1
2
system("cls");
cout << n << hex << endl; // print memory addres of n(int) 


system() runs a separate program. It has to find the program, allocate memory for it, load the program into the memory, run it, and then free up the memory. That's really expensive. If I reduce to loop count to 5,000, the program takes 22 seconds to run.

Changing system("cls") to instead print out the escape sequence to clear the screen (ESC [2J) the runtime drops to 19ms.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>

using namespace std;

int
main()
{
    const char esc = 27;
    for (int n = 0; n <= 5000; n += 4)   // from 0 to 500000 4 bytes
    {
        // system("clear");
        cout << esc << "[2J";
        cout << n << hex << endl;                // print memory addres of n(in\
t)
    }
    cout << esc << "[2J";
    // system("clear");
    cout << "finish" << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.