Skip to content

Commit fa57c5e

Browse files
committed
no busy waiting, improve cpu usage
1 parent 319f933 commit fa57c5e

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

Diff for: badapple.c

+35-11
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#include <windows.h>
66
#include <assert.h>
77
#include "zlib.h"
8+
#define TIMESTAMP(t) (long long)((t).QuadPart)
89
#define FRAME_COUNT 5479
9-
#define FRAME_TIME 40
10-
#define frame_time(x) ((x + 1) * FRAME_TIME)
10+
#define TIME_PRE_PRAME 40
11+
#define FRAME_TIME(x, freq) (((x) + 1) * TIME_PRE_PRAME * ((freq) / 1000))
1112
#define CHUNK 16384
1213
unsigned long long frame[5500][142][5];
1314
void GotoXY(int x, int y)
@@ -91,6 +92,7 @@ void output_frame(int i)
9192
}
9293
puts(frame_buf);
9394
}
95+
9496
int main(void)
9597
{
9698
FILE *fp = fopen("deflate.dat", "rb");
@@ -104,20 +106,42 @@ int main(void)
104106
{
105107
return zlibret;
106108
}
109+
LARGE_INTEGER qpc;
110+
long long freq = 0;
111+
if(!QueryPerformanceFrequency(&qpc))
112+
{
113+
printf("QueryPerformanceFrequency fail!\n");
114+
return 0;
115+
}
116+
else
117+
{
118+
freq = TIMESTAMP(qpc);
119+
}
107120
WinExec("mpg123.exe -q bad_apple.mp3", SW_HIDE);
108-
clock_t start, end;
109-
clock_t print_start, print_end;
110-
start = clock();
121+
122+
long long start, end;
123+
long long print_start, print_end;
124+
QueryPerformanceCounter(&qpc);
125+
start = TIMESTAMP(qpc);
111126
for (int i = 0; i < FRAME_COUNT; i++)
112127
{
128+
QueryPerformanceCounter(&qpc);
129+
print_start = TIMESTAMP(qpc);
113130
GotoXY(0, 0);
114-
print_start = clock();
115131
output_frame(i);
116-
print_end = clock();
117-
while ((end = clock()) - start < frame_time(i))
118-
;
119-
printf("frame%04d at %06d, offset %2d. frame -> time %2d, print time %2d\n",
120-
i, end - start, end - start - frame_time(i), end - print_start, print_end - print_start);
132+
QueryPerformanceCounter(&qpc);
133+
print_end = TIMESTAMP(qpc);
134+
while (1)
135+
{
136+
QueryPerformanceCounter(&qpc);
137+
end = TIMESTAMP(qpc);
138+
if(end - start < FRAME_TIME(i, freq))
139+
Sleep(1);
140+
else
141+
break;
142+
}
143+
printf("frame%04d, tick per-second %lld, tick %010lld, frame tick %08lld, print tick %08lld\n",
144+
i, freq, end - start, end - print_start, print_end - print_start);
121145
}
122146
return 0;
123147
}

Diff for: badapple.exe

134 KB
Binary file not shown.

0 commit comments

Comments
 (0)