我的意图是创建一个程序,分析不同数量的样本,每个样本都有时间限制。所以定时器必须重置每个样本。同样,必须是一个while循环,它确保程序不会运行太久。重置计时器的每次迭代
代码:
#include <cstdlib>
#include<stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <unistd.h>
//All functions declaration
int main(){
//function which read all the sample to analyse
for (int sample=0;sample<numOfSamples;sample++){
srand(time(NULL));
float tMax = tMax();//A function which calculates the maxium time for that sample
clock_t t;
t=clock();
//Some more functions which are not important
time= (clock()-t)/CLOCKS_PER_SEC;
while(time<tMax){
//Do different functions
time=(clock()-t)/CLOCKS_PER_SEC;
cout<<"Timer: "<<time<<endl;
}//End of while
}//End of For Loop (Samples)
}//ENd of main
这是我的代码,当你看到有我的计时器没有重启但因为我不知道如何使用它。但是这是计时器的主要问题,它总是0.所以它总是低于tMax。
如何重置计时器并获得大于0的值?
Wy你需要重置计时器吗?只需测量时间和使用时间差异即可。顺便说一句你在哪个系统上运行?如果在Linux上,阅读[time(7)](http://man7.org/linux/man-pages/man7/time.7.html) –
C++ 11 has [''](http:// en.cppreference.com/w/cpp/chrono)标准标题 –