Unsolved How can I get a difference between two DateTime intervals in micro and nano seconds?
-
am getting milliseconds like this
quint64 ITime::elapsed() { oEndTime = QDateTime::currentDateTime(); quint64 elapsed = qAbs(oStartTime.date().daysTo(oEndTime .date())); elapsed *= static_cast<quint64>(24); // days to hours elapsed *= static_cast<quint64>(60); // hours to minutes elapsed *= static_cast<quint64>(60); // minutes to seconds elapsed *= static_cast<quint64>(1000); // seconds to milliseconds elapsed += qAbs(oStartTime.time().msecsTo(oEndTime .time())); return elapsed; }
am getting milliseconds here , but i want micro and nano seconds in difference .
am using Qt 4.3.2 version , in visual studio 2005.
-
Hi,
You can't, dates are calculated as milliseconds since epoch.
If you want something with a higher level of details to measure a duration then you should take a look at std::chrono::duration.
Note that you are currently developing with a pretty outdated version of Qt as well as compiler. If possible, you should upgrade.
-
@SGaist chrono is not working for me
i try like this with #include <chrono>auto start = std::chrono::high_resolution_clock::now(); auto finish = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << "ns\n";
-
in what way "not working" ?
#include <iostream> #include <chrono> using namespace std; int main() { cout << chrono::high_resolution_clock::period::den << endl; auto start_time = chrono::high_resolution_clock::now(); int temp; for (int i = 0; i< 242000000; i++) temp+=temp; auto end_time = chrono::high_resolution_clock::now(); cout << chrono::duration_cast<chrono::seconds>(end_time - start_time).count() << ":"; cout << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << ":"; return 0; }
-
@mrjj fatal error C1083: Cannot open include file: 'chrono': No such file or directory
error is showing like this -
@srikanth
Ok, sounds like you dont have it then.
VS 2005 is epic old compiler wise.
Its not possible to use c++11 with it. -
@mrjj is there any way to get precision time difference (micro & nano) in my environment , am struck here , please help me to get out from this , thanks in advance :)
-
@srikanth
Im not sure nano is an option unless high precision timer is available.You can google on ways to time functions etc in c++
Sounds like you want something like that
http://stackoverflow.com/questions/15092504/how-to-time-a-function-in-milliseconds-without-boosttimerThere are also native windows functions etc but i do not know what task or requirements you have.
There are a lot on google.
-
boost chrono is an option, I don't think it needs C++11.
Do you need to get the difference between 2 timers (so you have to get the current time) or is it the difference of some static data-points you have? in the second case you can handle it quite simply manually
-
You can use
#include <QElapsedTimer>
its has the function
qint64 QElapsedTimer::nsecsElapsed() const
that is nano seconds since last started.
Available since Qt 4.7. -
That's what I would have proposed except his environment uses Qt 4.3.2
-
@SGaist
Oh, I missed that, sorry.
For some reason all I thought was "pre Qt5.0 so it fits" . -
Hi
He is trying the boost chrono as @VRonin suggest but
im not sure building it with VS 2005 is great joy. ? :) -
@mrjj The current release uses C++11 but boost version 1.47.0 does not. I also think boost::chrono and std::chrono are the same thing
-
@VRonin
Ah and such old version might have makefile for Vs 2005 included.
We cross fingers :=) -
can i use chrono in VS 2005 , am unable to get nanoseconds is there anyway to get nanoseconds in vs 2005 & Qt 4.3.2
-
@srikanth As @VRonin said you can use Boost 1.47.0 (boost::chrono: https://theboostcpplibraries.com/boost.chrono).
-
@jsulm i downloaded the boost.zip file from this link
[http://www.boost.org/users/history/version_1_63_0.html]unzip them and then add something like INCLUDEPATH += C:/boost/boost_1_63_0/ to my pro file , if i run this code then it is getting error like this "fatal error LNK1104: cannot open file 'libboost_chrono-vc80-mt-gd-1_63.lib' " so what can i do now ? to get this lib file .
-
@srikanth
Try the 1.47.0 one.
1_63 might be too new for VS 2005. -
@srikanth You need to link to the library mentioned in the error message:
LIBS += -LPATH_TO_BOOST_LIBS -lboost_chrono-vc80-mt-gd-1_63