How to know how much time block will take ?

This is a discussion on How to know how much time block will take ? within the C++ Programming forums, part of the General Programming Boards category; Hello,.. After a lecture in Data structures i had a strange question : How can i know how much time ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    How to know how much time block will take ?

    Hello,..

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

    since using
    Code:
    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 ?
    http://bsh83.blogspot.com

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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!
    CornedBee

    "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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    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. 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 Cprogramming.com
    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, 09:12 PM

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