#include
#include
#include "windows.h"
#include "processthreadsapi.h"
// Returns CPU process time in milliseconds
static long long get_cpu_time()
{
union FiletimeUnion
{
FILETIME as_filetime;
long long as_llong;
};
// Get process user and kernel times
FILETIME create_time, exit_time;
FiletimeUnion user_time, kernel_time;
GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time.as_filetime, &user_time.as_filetime);
// Return sum of user and kernel times in milliseconds
return (user_time.as_llong + kernel_time.as_llong) / 10000;
}
int main()
{
// Measure start times
const auto cpu_time_start = get_cpu_time();
const auto wall_time_start = std::chrono::steady_clock::now();
// Run application with a single thread
// ...
// Measure end times
const auto cpu_time_end = get_cpu_time();
const auto wall_time_end = std::chrono::steady_clock::now();
// Get elapsed times in milliseconds
const auto cpu_time_in_ms = static_cast(cpu_time_end - cpu_time_start);
const auto wall_time_in_ms = std::chrono::duration_cast(wall_time_end - wall_time_start).count();
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/77515000/workarounds-for-incorrect-cpu-thread-and-process-times-on-windows-11[/url]
Очевидно, существует много жалоб на некорректное использование ЦП в Windows 11, см., например, здесь https://answers.microsoft.com/en-us/windows/forum/all/windows-11-22h2-all-monitoring-softwares-report/3e669b84-c0ac-427f-a29e-86debf2db43f?page=2 и здесь https://www.reddit.com/r/Windows11/comments/yl3vxc/monitoring_programs_reports_wrong_cpu_load_on/?rdt=44755 К сожалению, это также влияет на время процессов ЦП и потоков ЦП. После обновления до Windows 11 измеренное время процессора намного меньше времени настенных часов. Вот как я измеряю время процессора: [code]#include #include #include "windows.h" #include "processthreadsapi.h"
// Returns CPU process time in milliseconds static long long get_cpu_time() { union FiletimeUnion { FILETIME as_filetime; long long as_llong; };
// Get process user and kernel times FILETIME create_time, exit_time; FiletimeUnion user_time, kernel_time; GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time.as_filetime, &user_time.as_filetime);
// Return sum of user and kernel times in milliseconds return (user_time.as_llong + kernel_time.as_llong) / 10000; }
int main() { // Measure start times const auto cpu_time_start = get_cpu_time(); const auto wall_time_start = std::chrono::steady_clock::now();
// Run application with a single thread // ...
// Measure end times const auto cpu_time_end = get_cpu_time(); const auto wall_time_end = std::chrono::steady_clock::now();
// Get elapsed times in milliseconds const auto cpu_time_in_ms = static_cast(cpu_time_end - cpu_time_start); const auto wall_time_in_ms = std::chrono::duration_cast(wall_time_end - wall_time_start).count();