Calculate time

This is a discussion on Calculate time within the C++ Programming forums, part of the General Programming Boards category; Hi Im looking for a method that can calculate the time that it takes to export a piece of code, ...

  1. #1
    Goo
    Goo is offline
    Registered User
    Join Date
    May 2009
    Posts
    1

    Calculate time

    Hi

    Im looking for a method that can calculate the time that it takes to export a piece of code, i.e. the speed like - O(N)


    Something like this (I know this is Java code, but I need it in C++):

    Code:
    QTimer t = new QTimer();
    t.start();
    //now a method that calls a method for each tick, eg. for each tick t, we call graph.redraw().
    
    //calculations
    
    t.stop();

    Can someone please help me?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can look in the header <ctime> to see what's available.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    O(N) can not be measured trivially by measuring time. O(N) for one algorithm could be 1000x times slower than anohter O(N) algorithm. All O(N) tells you is that if you double the number of elements, it takes twice as long - but it actually says NOTHING about the amount of time to perform the operations in themselves.

    Consider an two implementations sorting strings, where one implementation is copying the entire string, another copies just a pointer. For longer strings, that would make a big difference.

    As to measuring time precisely, you may want to look at clock(). However, that is a bit dependant on the OS and what your application is doing - Windows gives the amount of time passed, whilst in Linux/Unix it gives the amount of actual CPU time. It makes little difference if the CPU is 100% busy all the time. But if the program does for a lot of for example file I/O, it may not use the CPU 100%, so the number you get will be different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21