Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depends on your embedded platform and what type of timing you are looking for. For embedded Linux, there are several ways you can accomplish. If you wish to measure the amout of CPU time used by your function, you can do the following:</p> <pre><code>#include &lt;time.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define SEC_TO_NSEC(s) ((s) * 1000 * 1000 * 1000) int work_function(int c) { // do some work here int i, j; int foo = 0; for (i = 0; i &lt; 1000; i++) { for (j = 0; j &lt; 1000; j++) { for ^= i + j; } } } int main(int argc, char *argv[]) { struct timespec pre; struct timespec post; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &amp;pre); work_function(0); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &amp;post); printf("time %d\n", (SEC_TO_NSEC(post.tv_sec) + post.tv_nsec) - (SEC_TO_NSEC(pre.tv_sec) + pre.tv_nsec)); return 0; } </code></pre> <p>You will need to link this with the realtime library, just use the following to compile your code:</p> <pre><code>gcc -o test test.c -lrt </code></pre> <p>You may also want to read the man page on <code>clock_gettime</code> there is some issues with running this code on SMP based system that could invalidate you testing. You could use something like <code>sched_setaffinity()</code> or the command line <code>cpuset</code> to force the code on only one core.</p> <p>If you are looking to measure user and system time, then you could use the <code>times(NULL)</code> which returns something like a jiffies. Or you can change the parameter for <code>clock_gettime()</code> from <code>CLOCK_THREAD_CPUTIME_ID</code> to <code>CLOCK_MONOTONIC</code>...but be careful of wrap around with <code>CLOCK_MONOTONIC</code>.</p> <p>For other platforms, you are on your own.</p> <p>Drew</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload