c++ - counting time point in chrono library -
i'm using chrono library first time please lenient c:. want write simple time function return processor tics chrono. when try use clock()
function value time point initialized steady_clock::now()
, gcc says time_point
doesn't contain count()
member.
/home/siery/documents/project/changestatemachine/main.cpp|41|error: ‘struct std::chrono::time_point<std::chrono::_v2::steady_clock, std::chrono::duration<long long int, std::ratio<1ll, 1000000000ll> > >’ has no member named ‘count’|
here code:
float gettime() { float tic = 0.0; auto start = std::chrono::steady_clock::now(); tic = (float)start.count(); return tic; }
thank you!
edit:
gettime()
function should return current time calculate delta time update objects in program like:
fpasttime = fcurrenttime; fcurrenttime = gettime(); fdt = fcurrenttime - fpasttime; if (fdt >= 0.15f) fdt = 0.15f; update(fdt);
recommendation:
std::chrono::steady_clock::time_point gettime() { return std::chrono::steady_clock::now(); }
if want result of timing in terms of float-based seconds, this:
using fsec = std::chrono::duration<float>; auto t0 = gettime(); some_function(); auto t1 = gettime(); fsec delta = t1 - t0; std::cout << delta.count() << "s\n";
- you not responsible units conversions, therefore not introduce bugs in area.
- the code simple. simple == low risk.
- there not need
gettime()
wrapper.
edit:
#include <chrono> using namespace std::chrono; using fsec = duration<float>; using ftp = time_point<steady_clock, fsec>; ftp gettime() { return steady_clock::now(); } int main() { auto fpasttime = gettime(); auto fcurrenttime = gettime(); auto fdt = fcurrenttime - fpasttime; if (fdt >= 0.15s) fdt = 0.15s; // ... }
the 0.15s
requires c++14. in c++11 looks like:
if (fdt >= fsec{0.15}) fdt = fsec{0.15};
Comments
Post a Comment