안녕하세요 저는 스피드 큐브 타이머를 만들었습니다. 저는 시간 중심을 가졌지 만 그 시간이 너무 느리다는 것을 알았습니다. 1000에서 usleep 기능을 변경하려고 시도 했었습니다. 천천히, 어떤 생각?타이머 프로그램이 너무 느림
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int minutes = 0, milliseconds = 0, seconds = 0, x = 0, y = 0, text = 6, textminutes = 0, textseconds = 0, textmilliseconds = 0;
initscr();
while(1)
{
/*This block of code centers the text on the screen by incrementing each variable by one
for each number starting at ten, Then prints the time.*/
getmaxyx(stdscr,y,x);
if (seconds == 60 && minutes == 10){
textminutes += 1;
}
if (milliseconds == 1000 && seconds == 10){
textseconds += 0;
}
if (milliseconds == 10){
textmilliseconds += 1;
}
else if (milliseconds == 100)
{
textmilliseconds += 1;
}
else if(milliseconds == 1000)
{
textmilliseconds += 1;
}
int left_row = (x/2) - (3 + textminutes + textseconds + textmilliseconds/2);
mvprintw(y/2, left_row,"%d : %d : %d", minutes, seconds, milliseconds);
/*Sleep for 1 millisecond the increment the milliseconds
var i don't think that the timing is right though.
Then it refreshes and clears the screen to fetch the new contents.*/
usleep(1000);
milliseconds++;
if(milliseconds == 1000)
{
milliseconds = 0;
textmilliseconds -= 2;
seconds++;
if(seconds == 60)
{
seconds = 0;
textseconds -= 1;
minutes++;
}
}
refresh();
clear();
}
endwin();
return(0);
}
'time()'함수는 1 초의 세밀도를 제공합니다. 밀리 초 타이밍의 경우 POSIX 시스템에서는 ['clock_gettime()'] (http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html) 또는 ['gettimeofday()'] (http : //pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html) - 후자는 더 이상 쓸모가 없지만보다 광범위하게 사용할 수 있습니다 ('clock_gettime()'은 macOS Sierra에서는 사용할 수 있지만 이전 버전의 Mac OS X에서는 사용할 수 없습니다. 예). –
고마워, 나는 'clock_gettime' 제안에 대한 나의 대답을 변경했습니다. –
감사합니다. 나는'clock_gettime()'을 사용해야 할 것이다. –