Thread: clock vs gettimeofday

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    The 4ms lag is probably what I described, a system-wide scheduler lock for some "big job" in the kernel, where the kernel needs to ensure that the entire task is complete before it releases the lock. There are probably other ways to solve that sort of problem when writing the kernel, but the kernel was NEVER written to be good at real-time performance.

    My suggestion was to sleep a longer period to reduce the risk of colliding with other tasks - because if you "interrupt" the system 2000 times a second (0.5 ms), you will use up a fair amount of CPU time just doing that.

    --
    Mats
    I have two threads, both nanosleeping() for 0.5 ms. They both do partial drawing. First every 20 ms, second every 40 ms (i shifted startup of first by 5ms so they colide as little as possible). It's single core CPU celeron 1.6 Mhz. When under full load (most intesive scrolling), these two threads use 10%. Under normal drawing they use 3%. When i use bigger step, if they miss by more than 3ms, it's practicly visible. (It's all because of that slow library, i have these 'issues').

    EDIT: even 2ms lag if happens few times is more than visible!
    Last edited by rasta_freak; 08-07-2008 at 07:04 AM.

  2. #17
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    As for my part I have an algorithm that performs calculations on about 100MB of information from the main memory. It is performed in a quad core system using 4 threads. I have calculated time with gettimeofday().
    So I am looking to see if there would be a difference using clock(). I can always try it, but I want to know beforehand what would be the cause of this possible differences.

    So, I am wondering how clock() works for different instructions.
    -If you read from hard disk you said that clock won't calculate the time needed for the data to be loaded, right?
    -If you read from the main memory?
    -If you have multiple threads running in different cores, what would clock() measure compared to gettimeofday()?

    Sorry to keep asking, just haven't fully clarified the differences...

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you are reading from disk, there will be "no" time from "clock()", because the CPU is able to work for other processes in the system [if there is none, then it becomes the "idle" process that runs, and "clock()" for the idle process gets counted up].

    If you read "main memory" then the CPU is busy in your process, so that time is counted in clock().

    If multiple threads are running at the same time, clock() will increase for each thread.

    To measure application performance, you probably want to have BOTH "walltime" (gettimeofday() time) and CPU-time (clock() time), and use the combination of them to determine the overall application performance.

    --
    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.

  4. #19
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    OK, thanx a lot. That's is what I am going to do!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-28-2010, 06:30 AM
  2. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  3. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  4. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  5. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM