Jump to page: 1 2
Thread overview
time measurement under linux?
Jan 19, 2009
Trass3r
Jan 19, 2009
Daniel Keep
Jan 19, 2009
Trass3r
Jan 19, 2009
Bill Baxter
Jan 19, 2009
Trass3r
Jan 19, 2009
Bill Baxter
Jan 19, 2009
Jason House
Jan 19, 2009
Bill Baxter
January 19, 2009
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;
		}
	}
}
January 19, 2009

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
January 19, 2009
"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


January 19, 2009
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.
January 19, 2009
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;
> }
> }
> }

January 19, 2009
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
January 19, 2009
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.
January 19, 2009
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
January 19, 2009
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..)
January 19, 2009
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).
« First   ‹ Prev
1 2