PerformanceAwareProgramming/part2/listing_0072_cpu_timer_main.cpp

50 lines
1.3 KiB
C++

/* ========================================================================
(C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Please see https://computerenhance.com for more information
======================================================================== */
/* ========================================================================
LISTING 72
======================================================================== */
#include <stdint.h>
#include <stdio.h>
typedef uint64_t u64;
typedef double f64;
#include "listing_0070_platform_metrics.cpp"
int main(void)
{
u64 OSFreq = GetOSTimerFreq();
printf(" OS Freq: %llu\n", OSFreq);
u64 CPUStart = ReadCPUTimer();
u64 OSStart = ReadOSTimer();
u64 OSEnd = 0;
u64 OSElapsed = 0;
while(OSElapsed < OSFreq)
{
OSEnd = ReadOSTimer();
OSElapsed = OSEnd - OSStart;
}
u64 CPUEnd = ReadCPUTimer();
u64 CPUElapsed = CPUEnd - CPUStart;
printf(" OS Timer: %llu -> %llu = %llu elapsed\n", OSStart, OSEnd, OSElapsed);
printf(" OS Seconds: %.4f\n", (f64)OSElapsed/(f64)OSFreq);
printf(" CPU Timer: %llu -> %llu = %llu elapsed\n", CPUStart, CPUEnd, CPUElapsed);
return 0;
}