On Tue, 10 Aug 2010 14:37:22 +0200 casper....@sun.com wrote: > But if you call time() in a loop, then even the best performance > won't change the runtime: it will just call time() more times. > > Casper
Well, quite often you need to call time function in a loop. For example, you have a loop that accepts network packets, or connections and every so often you need to have current system time. The resolution of the timer can be specific to application, i.e. it could be seconds, milliseconds, or microseconds.a uint64_t current_time; while(1) { get_network_data(); current_time = update_current_time(); ... use_current_time(); } So in a tight loop, if update_current_time() calls clock_gettime() on every loop iteration, what would kill performance. POSIX.4 provides interval timers, but I think they can be problematic: 1. There is a limit on the total number of timers a process can have, that is TIMER_MAX in <limits.h> and this is usually 32. What if you need more than 32 timers? 2. Timer notification is sent via Unix signals, which sucks if your process is multi-threaded. Also, signal delivery is not particularly efficient. My solution was to develop a small Interval Time Sampling package, below are the prototypes: #ifndef _its_h #define _its_h #include <time.h> #include <stdint.h> #include <inttypes.h> typedef struct its *its_t; int32_t its_init(its_t *its, clockid_t clock_id, uint32_t interval); void its_destroy(its_t *its); uint64_t its_sample(its_t its); #endif /* _its_h */ When calling its_init(), interval specifies at what intervals to call clock_gettime(). No matter how often you call its_sample(), it will call clock_gettime() at the specified intervals. It does it by checking internal 64-bit counters, if the counter reaches a threshold, it calls clock_gettime(), otherwise it increments the counter. If the counter gets too high, it decrements it. This way it automatically adjusts to the frequency of loop iterations. Each thread has its own its_t object, so even if it's spinning in a tight loop, most of the time the cost of its_sample() call is a 64-bit increment instruction on a local variable. _______________________________________________ opensolaris-code mailing list opensolaris-code@opensolaris.org http://mail.opensolaris.org/mailman/listinfo/opensolaris-code