1

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~

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
ElanSaphir
  • 11
  • 1
  • 4
    Do you compile with optimizations? I wonder, why the inner loop, which is effectively a no-op, should take 11 ms. – Daniel Langr Jul 30 '20 at 12:52
  • It is clear that you compiled your code without optimizations. There is no point in doing that. However, it is expected for the scheduler to respond to your program behavior and adjust the scheduling. In particular, the program which calls `sleep` exhibits different pattern from the one which does not, and could well be treated differently. – SergeyA Jul 30 '20 at 12:58
  • FYI compiler has right to replace `while (ii--);` with `ii = -1`. Also `clock` resolution is poor. – Marek R Jul 30 '20 at 13:03
  • 1
    In general performance testing is hard topic especially when simple (very fast) code is under test. And your code does nothing, so compiler will effectively remove it (when optimization are enabled). Checkout this tool: https://quick-bench.com/ – Marek R Jul 30 '20 at 13:12
  • @mch, the OP processed the output and provided you a nice histogram of how many times each duration was reported. IT could have been clearer, granted, but not reason to mistrust the reported values – Jeffrey Jul 30 '20 at 13:14
  • Note that `clock()` measures *approximate CPU time*. It is approximate because it's hard to measure CPU time in the presence of context switches (such as the case in combination with `usleep`). Use `std::high_resolution_clock` instead, after enabling optimizations (and making sure the while-loop isn't optimized away ;) – rustyx Jul 30 '20 at 13:22
  • 2
    The `sleep` call will probably drop the CPU into its low power, low performance state. Transitioning out of this state when the CPU gets busy is not instantaneous. To avoid this you can set your system performance (using OS tools) to not reduce CPU clock speed when it is idle. – 1201ProgramAlarm Jul 30 '20 at 13:50
  • Thanks for the advice and comments. Yeah, I use `while (ii--)` to replace my time-consuming process and compile with no optimization. The testing is running on Ubuntu 18.04.4 and the compiler is GCC7.5 with std=c++14. Let me adjust my testing program and try again. :) – ElanSaphir Jul 31 '20 at 03:17

0 Answers0