-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMProfiler.cpp
More file actions
33 lines (26 loc) · 1.07 KB
/
Copy pathMProfiler.cpp
File metadata and controls
33 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "MProfiler.h"
#include <iostream>
#include <stdio.h>
MProfiler::MProfiler(const char* Name, bool PrintTimeOnStop) noexcept : MProfiler(std::string_view(Name), PrintTimeOnStop) {}
MProfiler::MProfiler(std::string_view Name, bool PrintTimeOnStop) noexcept : IsStopped(false), Name(std::move(Name)) {
ShouldPrintTimeOnStop = false;
(void)Restart();
ShouldPrintTimeOnStop = PrintTimeOnStop;
}
MProfiler::~MProfiler() noexcept { (void)Stop(); }
MProfiler::Nanoseconds CalculateNanos(auto Start, auto End) noexcept {
return std::chrono::nanoseconds(End - Start).count();
}
MProfiler::Nanoseconds MProfiler::Restart() const noexcept {
Nanoseconds prevTime = Stop();
Start = std::chrono::high_resolution_clock::now();
IsStopped = false;
return prevTime;
}
MProfiler::Nanoseconds MProfiler::Stop() const noexcept {
if (!IsStopped) End = std::chrono::high_resolution_clock::now();
IsStopped = true;
Nanoseconds nanos = CalculateNanos(Start, End);
if(ShouldPrintTimeOnStop) std::cout << "MProfiler \"" << Name << "\"\n" << "Time: " << nanos / 1.0e9L << std::endl;
return nanos;
}