获取 CPU 刻度和测量时间
Get CPU ticks and measured time
所以我想测量 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
当我在里面插入例如 Sleep() 函数时它工作正常但是当使用来自 math.h 的 sin 时我总是得到 0 次。这是为什么?我知道它可能已优化但 0?我想将它与其他 sin 实现进行比较,但我不确定这里有什么问题。
我认为最好使用内部滴答计数。我通常使用两个 类 来帮助我:
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)
在 strMeasureTime 中你会有时间。
计算100罪的时间很短,应该用
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;
相反。
此外,您的代码以毫秒为单位。我建议您使用 chrono::duration <double, nano> (diff).count()
,它使用纳秒以获得更高的精度。
来自文档:
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.
这意味着这 不是 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
当我在里面插入例如 Sleep() 函数时它工作正常但是当使用来自 math.h 的 sin 时我总是得到 0 次。这是为什么?我知道它可能已优化但 0?我想将它与其他 sin 实现进行比较,但我不确定这里有什么问题。
我认为最好使用内部滴答计数。我通常使用两个 类 来帮助我:
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)
在 strMeasureTime 中你会有时间。
计算100罪的时间很短,应该用
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;
相反。
此外,您的代码以毫秒为单位。我建议您使用 chrono::duration <double, nano> (diff).count()
,它使用纳秒以获得更高的精度。
来自文档:
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.
这意味着这 不是 CPU 您正在测量的刻度