On Thu, Apr 29, 2021, 10:07 AM yuta <yutr...@gmail.com> wrote: > Brennan, > Thank you for your advice. > > I checked links you shared and > https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html > I got elapsed time. Thank you. > > However I'm wondering why the elapsed time I got was not exactly the same > I expected to get. > I tried below. > > // ***** start > clock_t start, end; > double cpu_time_used; > > start = clock(); > usleep(1000000); // 1sec > end = clock(); > > cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC; > > printf("CPU time used: %f sec\n", cpu_time_used); > // ***** end > > What I got is ... > "CPU time used: 1.010000 sec" (I expected to get 1.000000 sec) > > Do you have any idea? >
Two things are going on here. 1. So if you did this on Linux you should get almost 0 because you used the CPU time measurement and so the thread is suspended and not utilizing the CPU. This is actually what I was talking about being not compliant about NuttX, it is giving you the delta in time since boot. 2. You are paying for a context switch and if you are not using the tickless OS configuration you also are at tick resolution which is normally 10ms which is what we see here. If you want the wall clock duration you should use elapsed time, even though on NuttX it may be the same. We likely will fix this in the future to be more standards compliant. https://www.gnu.org/software/libc/manual/html_node/Calculating-Elapsed-Time.html Hope that helps. --Brennan