Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I check how many times a function was called with a 3 seconds interval?
    primarykey
    data
    text
    <p>I would like to check how many times my function can be run in 3 seconds. I wrote that code:</p> <pre><code>#include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;time.h&gt; #include &lt;sys/time.h&gt; #include &lt;sys/resource.h&gt; double get_wall_time(){ struct timeval time; if (gettimeofday(&amp;time,NULL)){ // Handle error return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001; } int main(int argc, char **argv) { long iterations = 0; double seconds = 3.0; double wall0 = get_wall_time(), now; do { fun(a,b,c); now = get_wall_time(); iterations++; }while(now &lt; wall0+seconds); printf("%lu\n", iterations); return 0; } </code></pre> <p>But something tells me its not ok at all... I compared results with an executable from my teacher and turned out that his program does more iterations than mine in the same, 3-seconds time interval (<code>fun</code> is defined the same, teacher gave me its source, I only use it here).</p> <p>EDIT:</p> <p>Edited <code>while</code> loop but results still the same:</p> <pre><code> do { fun(a,b,c); iterations++; }while(get_wall_time() &lt; wall0+seconds); </code></pre> <p>EDIT:</p> <p>something like this? :</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;signal.h&gt; #include &lt;unistd.h&gt; /* 3 seconds */ volatile int seconds = 3; void handle(int sig) { --seconds; alarm(1); } int main() { signal(SIGALRM, handle); alarm(1); while(seconds) { fun(a,b,c); iterations++; } printf("%lu\n", iterations); return 0; } </code></pre>
    singulars
    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.
 

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