www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - time measurement under linux?

reply Trass3r <mrmocool gmx.de> writes:
I wrote a module to ease time measurement in my projects.
Does anyone know how to get elapsed milli- or nanoseconds under linux? 
Want to make it portable :)



module time;

version(Windows)
	import std.c.windows.windows;


long frequency;			/// frequency of the high performance counter
long startTime;			/// current measurement start time
long curTime;			/// current measurement end time
const bool hpcAvailable;/// high performance counter supported?

/**
  * initialize the high performance counter
  */
static this()
{
	version(Win32)
	{
		QueryPerformanceFrequency (&frequency);
		hpcAvailable = false;
		if (frequency)
		{
			hpcAvailable = true; // high performance counter not supported
			pragma(msg, "high performance counter available");
		}
	}
}

/**
  * start measurement
  */
void start()
{
	version(Windows)
	{
		if (hpcAvailable)
			QueryPerformanceCounter(&startTime);
		else
		{
			version(Win32)
				startTime = GetTickCount();
			version(Win64)
				startTime = GetTickCount64();
		}
	}
}

/**
  * get elapsed milliseconds
  */
double msecs()
{
	version(Windows)
	{
		if (hpcAvailable)
		{
			QueryPerformanceCounter(&curTime);
			return ((curTime-startTime) * 1000) / cast(double) frequency;
		}
		else
		{
			version(Win32)
				curTime = GetTickCount();
			version(Win64)
				curTime = GetTickCount64();
			return (curTime-startTime);
		}
	}
}

/**
  * get elapsed nanoseconds
  */
double nsecs()
{
	version(Windows)
	{
		if (hpcAvailable)
		{
			QueryPerformanceCounter(&curTime);
			return ((curTime-startTime) * 1000000) / cast(double) frequency;
		}
		else
		{
			version(Win32)
				curTime = GetTickCount();
			version(Win64)
				curTime = GetTickCount64();
			return (curTime-startTime) * 1000;
		}
	}
}
Jan 19 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Trass3r wrote:
 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
 [snip]
Check std.perf; it's documented, but sadly doesn't show up in the docs. -- Daniel
Jan 19 2009
next sibling parent reply Trass3r <mrmocool gmx.de> writes:
Daniel Keep schrieb:
 Check std.perf; it's documented, but sadly doesn't show up in the docs.
 
   -- Daniel
Cool, is similar to my design. Though GetTickCount64 could be added.
Jan 19 2009
parent Bill Baxter <wbaxter gmail.com> writes:
On Tue, Jan 20, 2009 at 2:19 AM, Trass3r <mrmocool gmx.de> wrote:
 Daniel Keep schrieb:
 Check std.perf; it's documented, but sadly doesn't show up in the docs.

  -- Daniel
Cool, is similar to my design. Though GetTickCount64 could be added.
That's odd. I made some updates to std.perf a while back and my understanding was that as a result of those Walter was going to put it on the list of documented modules. I guess he just forgot. --bb
Jan 19 2009
prev sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Jan 19, 2009 at 10:41 AM, Daniel Keep
<daniel.keep.lists gmail.com> wrote:
 Trass3r wrote:
 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
 [snip]
Check std.perf; it's documented, but sadly doesn't show up in the docs.
Namely, PerformanceCounter is crossplatform. (For some reason, HighPerformanceCounter only works on Windows, even though it uses the same mechanism as PerformanceCounter..)
Jan 19 2009
parent reply Trass3r <mrmocool gmx.de> writes:
Jarrett Billingsley schrieb:
 On Mon, Jan 19, 2009 at 10:41 AM, Daniel Keep
 <daniel.keep.lists gmail.com> wrote:
 Trass3r wrote:
 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
 [snip]
Check std.perf; it's documented, but sadly doesn't show up in the docs.
Namely, PerformanceCounter is crossplatform. (For some reason, HighPerformanceCounter only works on Windows, even though it uses the same mechanism as PerformanceCounter..)
HighPerformanceCounter only exists in phobos1 and seems to be redundant anyway (as you said, uses same mechanism).
Jan 19 2009
parent Bill Baxter <wbaxter gmail.com> writes:
On Tue, Jan 20, 2009 at 6:04 AM, Trass3r <mrmocool gmx.de> wrote:
 Jarrett Billingsley schrieb:
 On Mon, Jan 19, 2009 at 10:41 AM, Daniel Keep
 <daniel.keep.lists gmail.com> wrote:
 Trass3r wrote:
 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
 [snip]
Check std.perf; it's documented, but sadly doesn't show up in the docs.
Namely, PerformanceCounter is crossplatform. (For some reason, HighPerformanceCounter only works on Windows, even though it uses the same mechanism as PerformanceCounter..)
HighPerformanceCounter only exists in phobos1 and seems to be redundant anyway (as you said, uses same mechanism).
Oh yeh. That's sounding familiar. The phobos2 version of the file is the one with my changes, and the one Walter was supposed to make appear in the docs. Here's what I wrote to walter about the changes I made: """ I didn't end up changing the API. But I did eliminate some classes which I think were dead weight: the ScopePerformanceCounter that just calls start() for you, and the Windows-only HighPerformanceCounter. On versions of Windows that have QueryPerformanceCounter, HighPerformanceCounter and PerformanceCounter do exactly the same thing. On platforms that don't have it, PerformanceCounter uses a backup plan, while HighPerformanceCounter just fails. So there's pretty much no reason to use HighPerformanceCounter, unless you just like code that fails on particular versions of Windows. I was thinking about adding an RDTSC-based timer, but after reading about it a bit, it sounds like its use is discouraged these days, since it is not so reliable on multicore machines. """ ---bb
Jan 19 2009
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Trass3r" wrote
I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux? 
 Want to make it portable :)
Tango uses gettimeofday, which has microsecond resolution. See tango.time.StopWatch for functionality similar to your code. -Steve
Jan 19 2009
prev sibling parent reply Jason House <jason.james.house gmail.com> writes:
Trass3r wrote:

 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
The difficulty of doing platform independent timing is one of the many reasons that drove me to Tango...
 
 
 
 module time;
 
 version(Windows)
 import std.c.windows.windows;
 
 
 long frequency;			/// frequency of the high performance counter
 long startTime;			/// current measurement start time
 long curTime;			/// current measurement end time
 const bool hpcAvailable;/// high performance counter supported?
 
 /**
   * initialize the high performance counter
   */
 static this()
 {
 version(Win32)
 {
 QueryPerformanceFrequency (&frequency);
 hpcAvailable = false;
 if (frequency)
 {
 hpcAvailable = true; // high performance counter not supported
 pragma(msg, "high performance counter available");
 }
 }
 }
 
 /**
   * start measurement
   */
 void start()
 {
 version(Windows)
 {
 if (hpcAvailable)
 QueryPerformanceCounter(&startTime);
 else
 {
 version(Win32)
 startTime = GetTickCount();
 version(Win64)
 startTime = GetTickCount64();
 }
 }
 }
 
 /**
   * get elapsed milliseconds
   */
 double msecs()
 {
 version(Windows)
 {
 if (hpcAvailable)
 {
 QueryPerformanceCounter(&curTime);
 return ((curTime-startTime) * 1000) / cast(double) frequency;
 }
 else
 {
 version(Win32)
 curTime = GetTickCount();
 version(Win64)
 curTime = GetTickCount64();
 return (curTime-startTime);
 }
 }
 }
 
 /**
   * get elapsed nanoseconds
   */
 double nsecs()
 {
 version(Windows)
 {
 if (hpcAvailable)
 {
 QueryPerformanceCounter(&curTime);
 return ((curTime-startTime) * 1000000) / cast(double) frequency;
 }
 else
 {
 version(Win32)
 curTime = GetTickCount();
 version(Win64)
 curTime = GetTickCount64();
 return (curTime-startTime) * 1000;
 }
 }
 }
Jan 19 2009
next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Jan 19, 2009 at 1:46 PM, Jason House
<jason.james.house gmail.com> wrote:
 Trass3r wrote:

 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
The difficulty of doing platform independent timing is one of the many reasons that drove me to Tango...
std.perf has been around for ages <_< it's sad that it isn't documented.
Jan 19 2009
prev sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Tue, Jan 20, 2009 at 4:47 AM, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:
 On Mon, Jan 19, 2009 at 1:46 PM, Jason House
 <jason.james.house gmail.com> wrote:
 Trass3r wrote:

 I wrote a module to ease time measurement in my projects.
 Does anyone know how to get elapsed milli- or nanoseconds under linux?
 Want to make it portable :)
The difficulty of doing platform independent timing is one of the many reasons that drove me to Tango...
std.perf has been around for ages <_< it's sad that it isn't documented.
That's a lot of what I did, actually. A big reason it wasn't showing up in the docs was because it didn't have ddoc comments. So I wrote those, and also did something about portability I think. I don't recall what I did exactly, but there were some code changes too in addition to the ddoc changes. --bb
Jan 19 2009