Date: Thu, 20 Jan 2005 16:22:29 +0200 From: Alin-Adrian Anton <aanton@spintech.ro> To: Marco Trentini <mark@remotelab.org> Cc: freebsd-hackers@freebsd.org Subject: Re: clock time in milliseconds into a c program Message-ID: <41EFBEA5.50007@spintech.ro> In-Reply-To: <20050120004406.GF921@einstein.lab> References: <20050120004406.GF921@einstein.lab>
next in thread | previous in thread | raw e-mail | index | archive | help
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?
>
---snip---
#include <sys/time.h>
struct timeval tv1,tv2;
struct timezone tz1,tz2;
gettimeofday(&tv1,&tz1);
for (i=0;i<5000000;i++) {
tmp=tree_insert(myroot,i,i+10); // cycles
if (tmp) myroot=tmp; // cycles
}
gettimeofday(&tv2,&tz2);
fprintf(stderr,"Insertion of 5 billions lasted %d microseconds\n",
((tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec)));
---snip---
So you can measure the cycles multiple times then compute the average time.
For this snippet (which deals with a B+tree) i got results of the form
0.3445866564 microseconds, which means 344.5867 miliseconds.. etc..
I don't think there is a streight way to speed-up the default unix time
resolution, which is, as far as i know, in microseconds.
Regards,
--
Alin-Adrian Anton
GPG keyID 0x1E2FFF2E (2963 0C11 1AF1 96F6 0030 6EE9 D323 639D 1E2F FF2E)
gpg --keyserver pgp.mit.edu --recv-keys 1E2FFF2E
Never ask a man what OS he uses. If it's FreeBSD, he'll tell you.
If it's not, why embarrass him? ..I'm sorry..
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?41EFBEA5.50007>
