Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Ive been working on learning C++ and i cant seem to find a code for using more cpu on a multi core processor?

i have windows 7 64bit an intel i7 q720 unsure if that will make a difference in the coding.

Very new at learning c++ just started today so far i got some basic outputs on the screen, basic setting variables as predefined or as user defined, and some basic math functions. I had just gotten to learn how to make a loop in the program, so i have it counting from 0 to 100,000 to start and its counting at about 2500 per second per second but only using between 1-3% cpu what would be the coding to make it count faster using more cpu power and where would i put it, heres my current coding i may have some unnecessary code in there but i'm learning from youtube.

#include <iostream>

#include <cmath>

int main()

{

using namespace std;

int x;

for (x=0;x<100001;x++)

cout << x << endl;

system("pause");

return 0;

}

remember i'm learning from youtube please bare with my most likely amateur code. i do not know all the parameters, or syntaxes yet so please explain why the code works if you wouldn't mind or what the words in c++ mean to the programmer. Thanks :)

4 Answers

Relevance
  • Cubbi
    Lv 7
    1 decade ago
    Favorite Answer

    This program is so slow and it is not using as much CPU time as you expect because you're flushing the output stream on every iteration of the loop, which is what endl does. Many tutorials make the mistake of using it to indicate end of line. Change that to '\n', and compare:

    for (int x=0;x<100001;x++)

         cout << x << '\n';

    cout << endl; // you need this only once and only before system("pause")

    system("pause"); // which is not needed at all, just run the program from a console window

    now it should run much faster, but it would still be executing on one CPU. To parallelize, you will have to learn about threads and concurrent programming in general, which is a complicated topic.

  • 1 decade ago

    The reason the program counts only 2500 per second and uses only 3% of the CPU power is that it is printing the number to the screen on every iteration of the loop. Whenever it prints, the CPU has to stop and wait for the system and the display to catch up. If you took away the line displaying the number in the loop, it would count at a rate of hundreds of millions per second, and would probably complete in less than 1 millisecond (which is too short a time for a human to even notice), but during that 1 millisecond it would suck up all the extra power in a single core. If your system is 1-core, that would be 100% CPU usage. More likely your system is multicore, in which case the usage would only go to some integer fraction of 100% (i.e. 50% on a 2-core system, 25% on a 4-core system, etc), and because the display of CPU usage updates much less frequently than once every millisecond, you probably wouldn't get to see it spike even that high. Increasing the length of the loop to a billion or so would give you a few seconds and you'd probably get to see the full usage.

    That said, this still doesn't address the meat of your question, which is, how to you take advantage of more than one core at once? You absolutely can do it, and doing it involves something called 'threading'. Right now, your program runs only a single thread. What you have to do is set it up so that it starts multiple threads. Then the system will automatically pass off active threads to other cores. However, this is not as simple (not NEARLY as simple) as setting 'threads=4' or something and then running everything else normally. Multiple cores don't let you execute a single sequence of instructions faster. What they do is let you execute multiple sequences of instructions simultaneously. This introduces some really crazy problems that you haven't even conceived of so far. For instance, with a singlethreaded program, if I wrote this:

    int n=0;

    void blah()

    {

    n=5;

    cout<<n<<"\n";

    }

    and called blah(), I would be guaranteed to get '5' printed to the console. But with a multithreaded program, I don't get that guarantee anymore, because some other thread might come along and change the value of n between the line that sets n to 5 and the line that prints it to the console. Yes, 'between'. Now, if your two threads are never going to interact and everything they do treats separate sets of data, you'll never have that problem. But in the real world, you usually want the threads to interact in some way, and that means you have to start using some new mechanisms (such as locks) to make sure you can modify data safely and avoid breaking the program logic. Furthermore, because of the unpredictable way that schedulers work, you can have situations where an improperly written program will work perfectly 999 times out of 1000, and then fail catastrophically that 1 other time because a thread got scheduled at just precisely the wrong moment. This turns debugging multithreaded code into a colossal unholy nightmare that will make you want to scrape your own eyeballs out of their sockets with a rusty fork (no pun intended). Unfortunately, safe multithreaded programming is something any professional programmer (or even any serious amateur) will have to learn eventually, and it's becoming increasingly important as computers get more and more cores.

    You can check out pages such as the following for information on threading:

    http://en.wikibooks.org/wiki/C++_Programming/Threa...

    http://www.linuxselfhelp.com/HOWTO/C++Programming-...

  • Sd Sd
    Lv 5
    1 decade ago

    K. If the computer is busy with other stuff then that might be all that it can spare but, what you want to do is learn about threads and to be honest you don't need to worry about that until later(once you can actually write code).

    As far as this program goes, it's not particularly computer intensive and it's not programmed to use threads so it's only going to run on 1 of your 4 cores anyways.

  • 4 years ago

    Do you bypass to Church? in case you do, ask on the subject of different classes they must help human beings. they many times have a benevalance software, a detention center ministry, write notes to close in's, persons that are ill and a great style of different opportunities to do sturdy for persons. whilst you're actually not in Church, seek for one that is going by using the Bible, and baptizes for the forgivness of sin. You reported "the flaws I do suitable seems to be ineffective issues." no longer something you're able to do nicely is ineffective. What ever you're able to doing, do it to the suitable of your skill giving God the difference and compliment that he gave you some thing to be sturdy at. whilst you're sturdy at taking training and wearing them out, this is powerful. a great style of human beings will no longer be able to try this. You reported you have gotten greater will develop than people who've been working there longer than you have. so as that they must appreciate you and you should be doing a sturdy interest. do no longer hold close you head in sham, be pleased with your accomplishments. we've human beings at Church that positioned the travellers enjoying cards and penciles interior the lower back of the pews. this is a needed interest and one that must be performed. they could't instruct or do a great style of different issues yet they could try this interest and do it nicely. It takes all varieties of diverse abilities to make a Church run easily. If we've been all sturdy at coaching, who might do all the different jobs that must be performed? in case you should bypass lower back to college, that must be great. Even a commerce college could be sturdy. verify the hospitals, see what guidance you should be a paramedic. They keep lives each and all of the time. this is a few thing you will possibly savour doing. That way you're helping human beings plus getting paid for it. sturdy success and advantages

Still have questions? Get your answers by asking now.