获取CPU时钟和测量的时间 我想测量C++中的一些函数的时间,并且尝试了很多方法。这是我的最终代码: auto start = chrono::steady_clock::now(); clStart = clock(); for (int o=0; o<100;o++) { sin(o); // Sleep(1); } auto end = chrono::steady_clock::now(); clEnd = clock(); auto difTime = end-start; diff = chrono::duration <double,milli> (difTime).count(); //gives me 0 diffTicks = clEnd-clStart; //0 as well auto start=chrono::staid_clock::now(); clStart=时钟(); 对于(into=0;o
我认为最好使用内部滴答计数。我通常使用两个类来帮助我:获取CPU时钟和测量的时间 我想测量C++中的一些函数的时间,并且尝试了很多方法。这是我的最终代码: auto start = chrono::steady_clock::now(); clStart = clock(); for (int o=0; o<100;o++) { sin(o); // Sleep(1); } auto end = chrono::steady_clock::now(); clEnd = clock(); auto difTime = end-start; diff = chrono::duration <double,milli> (difTime).count(); //gives me 0 diffTicks = clEnd-clStart; //0 as well auto start=chrono::staid_clock::now(); clStart=时钟(); 对于(into=0;o,c++,time,cpu,clock,chrono,C++,Time,Cpu,Clock,Chrono,我认为最好使用内部滴答计数。我通常使用两个类来帮助我: class PACKAGE Profiler { public: Profiler() { totalTime=0; PCFreq = 0.0; numberOf =0.0; QueryPerformanceFrequency(&li); PCFreq = double(li.QuadPart)/1000.0;
class PACKAGE Profiler
{
public:
Profiler()
{
totalTime=0;
PCFreq = 0.0;
numberOf =0.0;
QueryPerformanceFrequency(&li);
PCFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
__int64 CounterStart;
LARGE_INTEGER li;
double totalTime;
double PCFreq;
long numberOf;
};
class PACKAGE Profiterol
{
private:
Profiler* m_myprofiler;
public:
Profiterol(Profiler* a_prof)
{
m_myprofiler = a_prof;
QueryPerformanceCounter(&m_myprofiler->li);
m_myprofiler->CounterStart = m_myprofiler->li.QuadPart;
m_myprofiler->numberOf++;
}
~Profiterol()
{
QueryPerformanceCounter(&m_myprofiler->li);
m_myprofiler->totalTime += double(m_myprofiler->li.QuadPart-m_myprofiler->CounterStart)/m_myprofiler->PCFreq;
}
};
然后,在代码中,您必须执行以下操作:
Profiler partial2;
{
Profiterol tmp(&partial2);
for (int o=0; o<100;o++)
{
sin(o);
// Sleep(1);
}
}
String strMeasureTime = String(partial2.totalTime) + "(" + String(partial2.numberOf)
Profiler partial2;
{
Profiterol tmp(和partial2);
对于(int o=0;o用于计算100 sin的时间非常短,您应该使用
auto start = chrono::steady_clock::now();
clStart = clock();
for (int o=0; o<100000;o++)
{
sin(o);
// Sleep(1);
}
auto end = chrono::steady_clock::now();
clEnd = clock();
auto difTime = end-start;
diff = chrono::duration <double,milli> (difTime).count();
diffTicks = clEnd-clStart;
auto start=chrono::staid_clock::now();
clStart=时钟();
对于(INTO=0;带INTO=0的oTry;o@Thomas…好吧,这是愚蠢的:D,谢谢,它似乎在工作。顺便说一句,sin真的不需要一个滴答声吗?我想时钟不会返回滴答声的数量?谢谢,我可能也会试试这个