Thread overview
Measure cpu time
May 07, 2021
Andre Pany
May 07, 2021
Imperatorn
May 07, 2021
Andre Pany
May 07, 2021
Dennis
May 07, 2021
Dennis
May 07, 2021
Andre Pany
May 07, 2021
Ali Çehreli
May 07, 2021
Alain De Vos
May 07, 2021
Alain De Vos
May 07, 2021
Ali Çehreli
May 07, 2021

Hi,

I try to convert some python code to D. On python I can get the cpu time for the current
process using

dcpu: float = time.process_time()

Is there some equivalent function in Phobos to get the cpu time on linux?

Kind regards
André

May 07, 2021

On Friday, 7 May 2021 at 08:25:43 UTC, Andre Pany wrote:

>

Hi,

I try to convert some python code to D. On python I can get the cpu time for the current
process using

dcpu: float = time.process_time()

Is there some equivalent function in Phobos to get the cpu time on linux?

Kind regards
André

Take a look at https://code.dlang.org/packages/getr

May 07, 2021

On Friday, 7 May 2021 at 08:37:47 UTC, Imperatorn wrote:

>

On Friday, 7 May 2021 at 08:25:43 UTC, Andre Pany wrote:

>

Hi,

I try to convert some python code to D. On python I can get the cpu time for the current
process using

dcpu: float = time.process_time()

Is there some equivalent function in Phobos to get the cpu time on linux?

Kind regards
André

Take a look at https://code.dlang.org/packages/getr

Thanks a lot :)

Kind regards
André

May 07, 2021

On Friday, 7 May 2021 at 08:25:43 UTC, Andre Pany wrote:

>

Is there some equivalent function in Phobos to get the cpu time on linux?

I don't think so, but you can use core.sys.posix.sys.resource: rusage.

If you want to use it on Windows as well, this is a code snippet I wrote for that:

/// Describes how much cpu time a process has been getting
struct CpuUsage {
	/// Time that the process has executed in kernel mode in microseconds
	ulong kernelTimeUs;

	/// Time that the process has executed in user mode in microseconds
	ulong userTimeUs;
}

bool getCpuUsage(out CpuUsage r) {
	version(Windows) {
		void* currentProcess = GetCurrentProcess();
		FILETIME creationTime; // time that process was created
		FILETIME exitTime; // undefined if process has not exited
		FILETIME kernelTime; // ru_stime
		FILETIME userTime; // ru_utime
		if (GetProcessTimes(currentProcess, &creationTime, &exitTime, &kernelTime, &userTime) == 0) {
			return false; // GetLastError();
		}
		ulong toMicroseconds(FILETIME ft)  {
			return (ft.dwLowDateTime | cast(ulong) ft.dwHighDateTime << 32) / 10;
		}
		r.kernelTimeUs = toMicroseconds(kernelTime);
		r.userTimeUs = toMicroseconds(userTime);
	} else version(Posix) {
		rusage rusageStruct;
		if (getrusage(RUSAGE_SELF, &rusageStruct) == -1) {
			return false; //errno
		}
		auto toMicroseconds(timeval t) {
			return cast(ulong) t.tv_sec * 1_000_000 + t.tv_usec;
		}
		r.kernelTimeUs = toMicroseconds(rusageStruct.ru_stime);
		r.userTimeUs = toMicroseconds(rusageStruct.ru_utime);
	}
	return true;
}

On Windows you need to link to "Psapi.lib", e.g. with pragma(lib, "Psapi.lib") or libs "Psapi.lib" in dub.sdl.

May 07, 2021

On Friday, 7 May 2021 at 08:52:13 UTC, Dennis wrote:

>

If you want to use it on Windows as well, this is a code snippet I wrote for that:

For completeness, the imports it uses:

version(Windows) {
	import core.sys.windows.windows;
	import core.sys.windows.psapi: PROCESS_MEMORY_COUNTERS, GetProcessMemoryInfo;
} else version(Posix) {
	import core.sys.posix.sys.resource: rusage, getrusage, RUSAGE_SELF, timeval;
} else {
	static assert(0, "unsupported platform");
}
May 07, 2021

On Friday, 7 May 2021 at 08:58:33 UTC, Dennis wrote:

>

On Friday, 7 May 2021 at 08:52:13 UTC, Dennis wrote:

>

If you want to use it on Windows as well, this is a code snippet I wrote for that:

For completeness, the imports it uses:

version(Windows) {
	import core.sys.windows.windows;
	import core.sys.windows.psapi: PROCESS_MEMORY_COUNTERS, GetProcessMemoryInfo;
} else version(Posix) {
	import core.sys.posix.sys.resource: rusage, getrusage, RUSAGE_SELF, timeval;
} else {
	static assert(0, "unsupported platform");
}

Thanks a lot.

Kind regards
André

May 07, 2021
On 5/7/21 1:25 AM, Andre Pany wrote:

> get the cpu time

For the sake of completeness, that kind of functionality is provided by operating systems as well. For example, on Linux, it is common to run the program through 'time' to get how much wall clock advanced, how much time was spent actually executing code on the CPU, and how much time was spent by the OS itself:

$ time my-program my-arguments ...

Check out 'man time' to see its command line switches. ('time --help' fails to execute '--help' :) )

Ali

May 07, 2021
I see,
https://dlang.org/phobos/core_stdc_time.html
does not contain the function : "clock_gettime"
Weird ...
May 07, 2021
Probably renamed ...
May 07, 2021
On 5/7/21 9:46 AM, Alain De Vos wrote:
> I see,
> https://dlang.org/phobos/core_stdc_time.html
> does not contain the function : "clock_gettime"
> Weird ...

stdc is about D bindings for C's std headers. I don't think clock_gettime is a standard C function. grep'ping under /usr/include/dmd reveals that clock_gettime is in this file:

/usr/include/dmd/druntime/import/core/sys/posix/time.d

and that is this module:

module core.sys.posix.time;

Ali