Note that there are some explanatory texts on larger screens.

plurals
  1. POInterpreting time command output on a multi threaded program
    text
    copied!<p>I have a multi threaded program and I am profiling time taken starting before all pthread_create's and after all pthread_join's.</p> <p>Now I find that this time, lets call it <strong>X</strong>, which is shown below in <em>"Done in xms"</em> is actually user + sys time of <strong>time</strong> output. In my app the number argument to a.out controls how many threads to spawn. <strong><em>./a.out 1</em></strong> spawn 1 pthread and <strong><em>./a.out 2</em></strong> spawns 2 threads where each thread does the same amount of work.</p> <p>I was expecting X to be the real time instead of user + sys time. Can someone please tell me why this is not so? Then this really means my app is indeed running parallel without any locking between threads.</p> <pre><code>[jithin@whatsoeverclever tests]$ time ./a.out 1 Done in 320ms real 0m0.347s user 0m0.300s sys 0m0.046s [jithin@whatsoeverclever tests]$ time ./a.out 2 Done in 450ms real 0m0.266s user 0m0.383s sys 0m0.087s [jithin@whatsoeverclever tests]$ time ./a.out 3 Done in 630ms real 0m0.310s user 0m0.532s sys 0m0.105s </code></pre> <p><strong>Code</strong></p> <pre><code>int main(int argc, char **argv) { //Read the words getWords(); //Set number of words to use int maxWords = words.size(); if(argc &gt; 1) { int numWords = atoi(argv[1]); if(numWords &gt; 0 &amp;&amp; numWords &lt; maxWords) maxWords = numWords; } //Init model model = new Model(MODEL_PATH); pthread_t *threads = new pthread_t[maxWords]; pthread_attr_t attr; void *status; // Initialize and set thread joinable pthread_attr_init(&amp;attr); pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE); int rc; clock_t startTime = clock(); for(unsigned i=0; i&lt;maxWords; i++) { //create thread rc = pthread_create(&amp;threads[i], NULL, processWord, (void *)&amp;words[i] ); if (rc){ cout &lt;&lt; "Error:unable to create thread: " &lt;&lt; i &lt;&lt; "," &lt;&lt; rc &lt;&lt; endl; exit(-1); } } // free attribute and wait for the other threads pthread_attr_destroy(&amp;attr); for(unsigned i=0; i&lt;maxWords; i++) { rc = pthread_join(threads[i], &amp;status); if (rc){ cout &lt;&lt; "Error:unable to join thread: " &lt;&lt; i &lt;&lt; "," &lt;&lt; rc &lt;&lt; endl; exit(-1); } } clock_t endTime = clock(); float diff = (((float)endTime - (float)startTime) / 1000000.0F ) * 1000; cout&lt;&lt;"Done in "&lt;&lt; diff &lt;&lt; "ms\n"; delete[] threads; delete model; } </code></pre>
 

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