Thread: How to know how much time block will take ?

  1. #1
    Registered User
    Join Date
    Apr 2007

    How to know how much time block will take ?


    After a lecture in Data structures i had a strange question :
    How can i know how much time a block will take ?

    since using
    time(long foo)
    twise will not revel how much it really take but how much it take between the start point and the end point (it can take 1 day to print hello world if your Kernel will take 99.99999 % of your cpu).

    How can i do it ?
    I am not sure that using unix time() function is the best.
    why Gaos didn't had a wife ?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    How can i know how much time a block will take ?
    Brush up on your second sight. You can make an estimate of how long a block will take, but you can't know.

    But of course, you actually want to know how long it actually took. time() is too imprecise, and it measures real time, as you said. (That's not necessarily a bad thing. If you need 1 day to print "Hello, World!" on a system where nothing but your program is running, you've got a problem no matter where the time was spent.) There are other ways. Using a good profiler is one way. Another is to use a function similar to GetProcessTimes() in Win32.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Or use clock() which is more precise than time, but also portable.

    As described, it can be difficult to judge how long a line of code, never mind a bunch of lines or a whole program, will take. On simpler processors, you could usually count the number of instructions [times number of time each instruction gets executed of course - whcih for arbitrary code can be difficult to figure out in itself] and guestimate from that how long the execution time would be. But with modern processors, this gets harder and harder, because some things can happen in parallel, other things cant, memory accesses are nowadays 10-100x slower than the execution of simple instructions, etc, etc.

    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. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. 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
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM