From owner-freebsd-hackers@FreeBSD.ORG Thu Jan 20 02:12:05 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5FB9016A4CE for ; Thu, 20 Jan 2005 02:12:05 +0000 (GMT) Received: from node15.coopprint.com (node15.cooperativeprinting.com [208.4.77.15]) by mx1.FreeBSD.org (Postfix) with SMTP id 57B9243D48 for ; Thu, 20 Jan 2005 02:12:04 +0000 (GMT) (envelope-from ryans@gamersimpact.com) Received: (qmail 69184 invoked by uid 0); 20 Jan 2005 02:10:07 -0000 Received: from unknown (HELO ?192.168.0.5?) (63.231.157.250) by node15.coopprint.com with SMTP; 20 Jan 2005 02:10:07 -0000 Message-ID: <41EF137D.1090100@gamersimpact.com> Date: Wed, 19 Jan 2005 20:12:13 -0600 From: Ryan Sommers User-Agent: Mozilla Thunderbird 0.7.3 (Windows/20040803) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Marco Trentini References: <20050120004406.GF921@einstein.lab> In-Reply-To: <20050120004406.GF921@einstein.lab> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit cc: freebsd-hackers@freebsd.org Subject: Re: clock time in milliseconds into a c program X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Jan 2005 02:12:05 -0000 Marco Trentini wrote: > Hi, I need to clock the function execution time into a C > program. I know /usr/include/time.h library but I need to > clock the time in milliseconds. > > Any suggestions, links? > Are you looking for how long the processor spent executing your code? Or the difference in wall time between when it enters the function and when it exits the function? The difference will be subtle, but will still be there. The problem with using wall time (the amount of time elapsed on the clock on the wall) is that it doesn't factor in context switches. In 1 second of wall time your code might only have been executing for a very small piece of it. Other programs could have been scheduled to run, the kernel could be doing a lot of system maintenance, all will eat time slices, and none will be shown if you just just look at the wall clock time. However, if all you want if to look at where a bottleneck is, or how long your program is spending in a given function, you could just generate a profile. It might be useful to know if you are looking for wall time or CPU time spent in those functions. At any rate, I'd suggest looking at microtime or nanotime. the (struct timeval) that microtime uses is defined in /usr/include/sys/_timeval.h if I remember. There are a lot of good guides to time keeping and accuracies. You might check out http://people.freebsd.org/~phk/ I remember him having a section on his timekeeping hobbies. -- Ryan Sommers ryans@gamersimpact.com