CUMeasureTimer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019-current Getnamo. All Rights Reserved
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. //Toggle to enable/disable timing code logs
  5. #define ENABLE_CUPRECISE_TIMER 1
  6. /**
  7. * C++ Utility Timer class. Multiple categories can be used simultaneously.
  8. *
  9. * Usage:
  10. * FCUMeasureTimer::Tick(TEXT("MyMeasurementCategory"));
  11. * //Your code
  12. * FCUMeasureTimer::Tock(TEXT("MyMeasurementCategory")); //This will log the time taken in miliseconds
  13. *
  14. * optionally get the result and handle logging manually
  15. * double Elapsed = FCUMeasureTimer::Tock(TEXT("MyMeasurementCategory", false);
  16. */
  17. class COREUTILITY_API FCUMeasureTimer
  18. {
  19. public:
  20. /**
  21. * Start a timer for given category
  22. */
  23. static void Tick(const FString& LogMsg = TEXT("TimeTaken"));
  24. /**
  25. * Returns time taken in milliseconds (to micro precision). This function will also log the time taken
  26. */
  27. static double Tock(const FString& LogMsg = TEXT("TimeTaken"), bool bShouldLogResult = true);
  28. private:
  29. double Then;
  30. };
  31. /**
  32. * Wrapper for FCUMeasureTimer calls.
  33. *
  34. * Usage:
  35. * {
  36. * //code you do not wish to measure
  37. *
  38. * FCUScopeTimer Timer(TEXT("My Message"));
  39. *
  40. * //your code
  41. * }
  42. * It will log duration when you exit the scope
  43. */
  44. class COREUTILITY_API FCUScopeTimer
  45. {
  46. public:
  47. FCUScopeTimer(const FString& InLogMsg);
  48. ~FCUScopeTimer();
  49. private:
  50. FString LogMessage;
  51. };