Thread: Virtual Clock

  1. #1
    Registered User nabeshin's Avatar
    Join Date
    Nov 2009
    Posts
    4

    Exclamation Virtual Clock

    hi to everyone.

    I have a singular problem and I don't know where search...

    I would create a program that uses minimal CPU and it can incrementate the value of a variable every millisecond, with the best precision possible.

    In this moment i use "nanosleep" in my program, where it sleeps for a millisecond, and when it wakes up, it increment the variable .It work well, but i want find a new solution to incrementate the precision of my program without that increase the use of the CPU.

    My first idea is to use a thread with a high priority to increase the precision of nanosleep...

    Thanks for your response.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There is no CPU usage by the process during sleep, and nanosleep is as precise as you will be able to get, so I don't think there is anything about this you could optimize or improve upon.

    Updating a single variable every millisec will barely use any processor power at all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User nabeshin's Avatar
    Join Date
    Nov 2009
    Posts
    4

    Post

    Uhm...

    This is a small example of the performance of nanosleep about 1ms:
    (I use getimeofday to verify this result)

    Ideal: Real: Difference:
    1000 1169 169
    1000 1109 109
    1000 1099 99
    1000 1095 95
    1000 1099 99
    1000 1095 95
    1000 1099 99
    1000 2777 1777
    1000 1102 102
    1000 1092 92
    1000 1152 152
    1000 1113 113
    1000 1145 145
    1000 1144 144
    1000 1070 70
    1000 1357 357
    1000 3623 2623
    1000 1073 73
    1000 1069 69

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This may differ from OS to OS, etc, since the hi-res timer is kernel based. Eg, getimeofday does not exist on my system.

    I would use time() for the yardstick anyway since that is a standard function should use the hardware clock.

    Anyway, I've done this test before too:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/timex.h>
    #include <time.h>
    
    int main() {
    	struct ntptimeval now;
    	static long start, last, ulast, umark;
    	long dif;
    	time_t RTCstart = time(NULL), RTCnow, RTClast;
    	int count;
    
    	ntp_gettime(&now);
    	start = now.time.tv_sec;
    	umark = now.time.tv_usec;
    	last = start;
    	while (1) {
    		ntp_gettime(&now);
    		RTCnow = time(NULL);
    		count = RTCnow-RTClast;
    		if (count) {
    			dif = now.time.tv_usec - umark;
    			printf("%ld seconds (RTC: %d) %ld/1000000\n",now.time.tv_sec-start,(int)(RTCnow-RTCstart),dif);
    			umark = now.time.tv_usec;
    		} 
    	last = now.time.tv_sec;
    	ulast = now.time.tv_usec;
    	RTClast = RTCnow;
    	}
    
    	return 0;
    }
    Nb. "sys/timex" is the same timer used by nanosleep.



    0 seconds (RTC: 0) 4/1000000
    0 seconds (RTC: 1) 328752/1000000
    1 seconds (RTC: 2) 0/1000000
    2 seconds (RTC: 3) 1/1000000
    4 seconds (RTC: 4) -999999/1000000
    4 seconds (RTC: 5) 999999/1000000
    5 seconds (RTC: 6) 0/1000000
    6 seconds (RTC: 7) -1/1000000
    7 seconds (RTC: 8) 1/1000000
    8 seconds (RTC: 9) 0/1000000
    9 seconds (RTC: 10) 0/1000000
    10 seconds (RTC: 11) -1/1000000
    11 seconds (RTC: 12) 0/1000000
    12 seconds (RTC: 13) 1/1000000

    Once aligned, this shows a max discrepancy of 1 millisecond.

    My general point is that the hi-res timer is based on system ticks, so it reflects the limit of resolution you will achieve.
    Last edited by MK27; 01-19-2010 at 11:54 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You need to use some kind of high-resolution, kernel space timer device. On most workhorse operating systems, the scheduling resolution is much more granular than 1 millisecond (on Linux, for instance, it's 10 ms by default so any attempt to get better precision than that from userspace is doomed to fail). To get precise millisecond timing you need to busy-wait or employ a special-purpose device like a real-time clock.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User nabeshin's Avatar
    Join Date
    Nov 2009
    Posts
    4

    Wink

    Ok, thanks you for your responce.
    Now I prove to use a kernel thread and see the difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM