【发布时间】:2013-10-20 07:23:59
【问题描述】:
如何在一秒钟内调用一个方法/函数 50 次然后计算花费的时间,如果花费的时间少于一秒,则休眠 (1-timespent) 秒。
下面是伪代码
while(1)
{
start_time = //find current time
int msg_count=0;
send_msg();
msg_count++;
// Check time after sending 50 messages
if(msg_count%50 == 0)
{
curr_time = //Find current time
int timeSpent = curr_time - start_time ;
int waitingTime;
start_time = curr_time ;
waitingTime = if(start_time < 1 sec) ? (1 sec - timeSpent) : 0;
wait for waitingTime;
}
}
我是 Timer API 的新手。谁能帮我知道什么是计时器API,我必须使用它来实现这一点。我想要可移植的代码。
【问题讨论】:
-
您希望精确到什么程度?您确定您的处理(尤其是
send_msg,可能会阻塞)总是花费不到 20 毫秒,即 50Hz 的周期?