2 #define _DEFAULT_SOURCE /* for timersub */
8 static void gettimestamp(struct timeval
* t
) {
12 static long mspassed(struct timeval
* t
) {
13 struct timeval now
, diff
;
14 gettimeofday(&now
, NULL
);
15 timersub(&now
, t
, &diff
);
16 return (diff
.tv_sec
* 1000) + (diff
.tv_usec
/ 1000);
20 puts("prog seconds_of_cpu_time_to_burn");
24 long parse_human_number(char* num
) {
28 size_t l
= strlen(num
);
30 if(l
&& l
< sizeof(buf
)) {
32 ret
= 1024 * 1024 * 1024;
33 else if(num
[l
-1] == 'M')
35 else if(num
[l
-1] == 'K')
40 return atol(buf
) * ret
;
47 int main(int argc
, char** argv
) {
51 if (argc
!= 2) return syntax();
52 waste_secs
= parse_human_number(argv
[1]);
55 while(mspassed(&start
) < waste_secs
* 1000) {}
57 printf("successfully wasted %ld seconds of your cpu time\n", waste_secs
);