Thread: Calculate time

  1. #1
    Registered User
    Join Date
    May 2009

    Calculate time


    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++):

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

    Can someone please help me?

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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    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.

    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
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM