C++ CPU clock


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: C++ CPU clock

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    Wink C++ CPU clock

    Hi. I made a CPU clock that I think tests the speed of your CPU simply. here's the code. I was hoping that someone would run it, and give me feed back on the speed. I only have one computer. How do you change the CPU speeed!!




    #include <iostream>

    using namespace std;

    int main()
    {

    int x;
    x = 0;
    int c;
    c = 0;

    cout << "start" << endl;

    for (int Y = 0; Y <= 9; Y++)
    {
    c++;
    cout << "second " << c << endl;
    while (x <= 1400000000) //gigahertz speed here.. mine is 1.4 gHz.
    x++;
    x = 0;
    }

    cout << "end";

    c = 0;

    cin.get();
    cin.get();


    }

  2. #2
    Join Date
    Oct 2009
    Posts
    2

    Also!

    if you run the TASK MANAGER at the same time. you use 100% CPU time! No joke.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Tight loops like this can never be reliable sources of time measurement because your can't predict what the system load will be when you run them -- other tasks running at the same time might consume CPU time as well.
    To access the CPU's clock you need to use low level system calls or assembly code.
    Danny Kalev

  4. #4
    Join Date
    Apr 2008
    Posts
    17
    Horrible code !
    On Windows, simply use well-known native system apis to get CPU Speed (see Win32 grp for C code)

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    Its not just the tight loop and all that, the code assumes you can increment an integer in one clock cycle (you can, but you also jumped, incremented the loop counter, and did several other operations, probably 10-20 instructions in all depending on your settings).

    It ignores the current load (as was mentiond) and also ignores pipelines, multi core cpu, non-fixed CISC instruction timing, debug/release/etc effects of compiler settings, and more.

    If you want the CPU speed , ask it, the CPU knows what type it is and how fast it is, so does your operating system. If you want to test it against the known speed, you need to really sit down and study the problem, this is not the way to do it, its not a simple problem.

Similar Threads

  1. Clock signal
    By stece in forum C++
    Replies: 3
    Last Post: 04-03-2008, 02:04 PM
  2. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 12:09 PM
  3. Clock using C++ & DirectX
    By Arkanops in forum C++
    Replies: 3
    Last Post: 04-14-2006, 04:44 AM
  4. PC CLOCK SYNCHRONIZING USING VISUAL BASIC
    By Al Rest in forum vb.announcements
    Replies: 0
    Last Post: 06-10-2002, 04:19 AM
  5. drwas a clock
    By zainorin in forum Java
    Replies: 0
    Last Post: 08-01-2000, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center