存储提升::计时器产生一个变量
Store boost::timer results in a variable
我正在写一些排序和搜索算法,并为大学作业测试它们,我必须得到处理不同测试所用的CPU时间和墙壁时间。以及个别时间安排。
我使用boost API来实现这一点,我的问题是我必须运行多个测试并获得平均时间,但我找不到将boost给我的结果存储在变量中的解决方案。
这是我的一个算法:
int CA1::binarySearch(vector<int> v, int target)
{
boost::timer::auto_cpu_timer t("%w");
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom) / 2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
编辑
@Surt我试着实现你的代码如下:
int main()
{
int size = 0;
cout << " enter the size of your vectorn";
cin >> size;
CA1 ca1(size);
ca1.DoTests;
system("pause");
return 0;
}
int CA1::binarySearch(vector<int> v, int target)
{
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom) / 2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
double measure(std::function<void()> function) {
auto start_time = std::chrono::high_resolution_clock::now();
function();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - start_time);
//std::cout << test << " " << static_cast<double>(duration.count()) * 0.000001 <<
// " ms" << std::endl;
return static_cast<double>(duration.count()) * 0.000001;
}
void CA1::DoTests() {
double time = measure(CA1::binarySearch(vectorUnordered,2));
cout << time << endl;
}
但我收到了错误,
error C3867: 'CA1::DoTests': function call missing argument list; use '&CA1::DoTests' to create a pointer to member
functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
知道我哪里错了吗?
编辑2
@Rob Kennedy
我试着实现你的代码std::bind,但我无法理解,我更改了我的代码如下:
double CA1::measure(std::function<void()> function) {
auto startCpu = boost::chrono::process_real_cpu_clock::now();
auto startWall = boost::chrono::process_system_cpu_clock::now();
function();
auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_real_cpu_clock::now() - startCpu);
auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_system_cpu_clock::now() - startWall);
double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
double wallTime = static_cast<double>(durationWall.count()) * 0.000001;
/*return static_cast<double>(duration.count()) * 0.000001;*/
cout << "Cpu time " << cpuTime << endl;
cout << "Wall time " << wallTime << endl;
return cpuTime;
}
void CA1::DoTests() {
auto time = measure(std::bind(binarySearch, vectorUnordered, 2));
}
引发错误:
error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member
我把std::bind放在正确的地方了吗?我需要更改measure()中的参数吗?它到底在做什么?
您使用boost::timer::auto_cpu_timer
,但应在此处使用boost::timer::cpu_timer
血腥细节
为了方便测量,只需将您最喜欢的计时器功能与std::chrono details:交换即可
double measure(std::function<void()> function) {
auto start_time = std::chrono::high_resolution_clock::now();
function();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - start_time);
return static_cast<double>(duration.count()) * 0.000001;
}
void Test1() {
... setup test
... call test
... validate return
}
void DoTests() {
double time = measure(Test1);
...
... profit!
}
相关文章:
- 用C++中的一个变量定义一个常量
- 一个变量的输入值也会保存到另一个变量中
- 将双精度变量设置为另一个变量的值
- 对具有相同方法的不同类使用一个变量
- 为什么一个变量获得与另一个值相同的值
- 尝试在 C++ 中为 ifstream 提供一个变量
- 类中的一个变量显示,但另一个不显示
- 声明一个变量,该变量在 c++ 或 c 中具有值,当程序终止时不会被销毁
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 如何将一个变量用于父类和派生类
- 如何在循环中使用scanf,将值存储到一个变量中,然后打印出来?
- 我如何将一个变量与另一个变量进行比较,例如我想如果(var1 > var2 x 1),然后执行此 c++
- 如何在一个函数中定义一个变量,并在另一个函数中访问和更改它?(C++)
- 如果一个变量在它之前释放了另一个(相同的数据类型)变量,如何将其分配给内存?
- 迭代器或反向器的一个变量
- fstream库,试图创建一个变量名为(c++)的文件
- 如何在 c++ 中将两个不同类型的变量分配给一个变量
- 如何将一些变量放在一个变量中?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 如何用索引命名一个变量来存储输入 mxArray?