Why usleep affects the later code performance?

191 Views Asked by At

I'm new to performance analysis and I had a performance problem in my system.
The code snippet is like

int main()
{
    for(int i {0}; i < 20000; ++i)
    {
        //usleep(30*1000);

        clock_t start = clock();
        int ii = 10000000;
        while (ii--);
        clock_t end = clock();

        double cpu_time_elapsed = ((double) (end - start)) / CLOCKS_PER_SEC * 1000;
        fprintf(stdout, "%0.*f %10ld - %10ld\n", 0, cpu_time_elapsed, end, start);
    }

    return 0;
}

And I made a couple of tests: one will comment the usleep line and another won't.
The output results are:

(processed output showing the duration and the number of times this duration was measured)

(no sleep: almost consume 11ms)
11 -- 19860
12 -- 139
13 -- 1

(sleep 30ms, widely dispersed)
11 -- 337
12 -- 205
13 -- 391
14 -- 404
15 -- 344
16 -- 133
17 -- 409
18 -- 84
19 -- 279
20 -- 254
21 -- 153
22 -- 669
23 -- 1275
24 -- 1679
25 -- 2609
26 -- 2520
27 -- 594
28 -- 2131
29 -- 1533
30 -- 123
31 -- 1985
32 -- 219
33 -- 431
34 -- 750
35 -- 32
36 -- 337
37 -- 15
38 -- 50
39 -- 20
40 -- 25
41 -- 7
42 -- 2
43 -- 1

What's the reason "usleep" made such a huge effect for the later while (ii--);?
And how can I do to elimiate/mitigate the effect?

Thanks~

0

There are 0 best solutions below