Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <chrono>
  2.  
  3. // Use some clever syntax to allow GetProcessTimeInSeconds() to work from pre-main() initalizers.
  4. // (in some cases the OS might init s_ProcessStartTimeSecs as 0 prior to invoking the runtime initializer
  5. // that gives it a real value... but probably not. Almost all operating systems will init the boolean
  6. // to zero prior to init() however, so that's what I do this little juggle).
  7. bool s_ProcessStartInit = 0;
  8. double s_ProcessStartTimeSecs = ((s_ProcessStartInit=1), std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
  9.  
  10. double GetProcessTimeInSeconds()
  11. {
  12. // Windows don't really have a concept of "process time" eg. individual processes cannot be suspended/resumed
  13. // individually. Suspend/resume (aka, sleep) occurs on a system-wide scale. So it should be fine enough to
  14. // return whatever std::chrono::high_resolution_clock queries.
  15.  
  16. // STL chrono is the most retarded thing ever. who the fuck cares about all this bullshit implicit conversion and
  17. // billions of units of measuremenets and ratios and under-the-hood conversion rounding that will make everyone's
  18. // life suck? Just convert to double, normalized to seconds, and then the world of time-keeping is easy. --jstine
  19.  
  20. if (!s_ProcessStartInit) return 0;
  21. return std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - s_ProcessStartTimeSecs;
  22. }
  23.  
  24. double GetProcessTimeInMsec()
  25. {
  26. // ... apparently because specifying std:milli as a template parameter is more readable and makes more sense than
  27. // taking a double result and multiplying it by 0.001. (/sarcasm)
  28. //
  29. // Why is chrono so overly optimized for integer maths? I mean I know the original API was developed in the late
  30. // 90's as part of BOOST when AMD still had a shit FPU unit and you needed to do all your maths as integers to
  31. // avoid grotesque bottlenecks. But why is this part of the STL standard? It's so wierdly hyper-optimized for
  32. // solving specific problems to specific (now out-dated) platforms. Even mobile has no trouble with double-
  33. // precision FPUs. --jstine
  34. if (!s_ProcessStartInit) return 0;
  35. return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - (s_ProcessStartTimeSecs*1000);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement