system function in a while loop

I noticed when I use the system function to clear a screen in a while loop my computer seems to get really loud like the processor is working a lot harder and when I close the program every thing goes back to normal,I wonder why this happens,

I will start off by guessing because maybe there is so much system calls being made in a short period of time?

here is an example with a simple console clock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
  struct tm* t2;
    time_t timeNow2;

    // time in a loop we will need to keep creating new tm objects to get current time

    while(true){

        timeNow2 = time(0);
        t2 = localtime(&timeNow2);
        cout << t->tm_hour << ":" << t->tm_min << ":" << t->tm_sec << endl;

        system("cls");
    }
}
std::system()
Calls the host environment's command processor (e.g. /bin/sh, cmd.exe, command.com) with the parameter command.
http://en.cppreference.com/w/cpp/utility/program/system

This would involve the creation of a new process (an expensive operation) for each iteration of this busy loop.
Maybe you need #include <iostream> because you need the cout << t->tm_hour << ":" << t->tm_min << ":" << t->tm_sec << endl;
Not only are you spawning a new process repeatedly, but that loop is also executing as fast as possible, resulting in 100% utilization of the core it is running on. This will heat up the processor, resulting in higher fan speeds.
Registered users can post here. Sign in or register to post.