digitalmars.D.learn - Measure cpu time
- Andre Pany (11/11) May 07 2021 Hi,
- Imperatorn (2/13) May 07 2021 Take a look at https://code.dlang.org/packages/getr
- Andre Pany (4/21) May 07 2021 Thanks a lot :)
- Dennis (48/50) May 07 2021 I don't think so, but you can use `core.sys.posix.sys.resource:
- Dennis (14/16) May 07 2021 For completeness, the imports it uses:
- Andre Pany (4/20) May 07 2021 Thanks a lot.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (10/11) May 07 2021 For the sake of completeness, that kind of functionality is provided by
- Alain De Vos (4/4) May 07 2021 I see,
- Alain De Vos (1/1) May 07 2021 Probably renamed ...
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/12) May 07 2021 stdc is about D bindings for C's std headers. I don't think
Hi, I try to convert some python code to D. On python I can get the cpu time for the current process using ``` python 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 ``` python 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:Thanks a lot :) Kind regards AndréHi, I try to convert some python code to D. On python I can get the cpu time for the current process using ``` python 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: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: ```D /// 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: ```D 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:Thanks a lot. Kind regards André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: ```D 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 5/7/21 1:25 AM, Andre Pany wrote:get the cpu timeFor 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
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
May 07 2021