linux - Interpreting time command output on a multi threaded program -


i have multi threaded program , profiling time taken starting before pthread_create's , after pthread_join's.

now find time, lets call x, shown below in "done in xms" user + sys time of time output. in app number argument a.out controls how many threads spawn. ./a.out 1 spawn 1 pthread , ./a.out 2 spawns 2 threads each thread same amount of work.

i expecting x real time instead of user + sys time. can please tell me why not so? means app indeed running parallel without locking between threads.

[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

int main(int argc, char **argv) {    //read words   getwords();    //set number of words use   int maxwords = words.size();   if(argc > 1) {      int numwords = atoi(argv[1]);      if(numwords > 0 && numwords < maxwords) maxwords = numwords;   }    //init model   model = new model(model_path);   pthread_t *threads = new pthread_t[maxwords];   pthread_attr_t attr;   void *status;    // initialize , set thread joinable   pthread_attr_init(&attr);   pthread_attr_setdetachstate(&attr, pthread_create_joinable);    int rc; clock_t starttime = clock();    for(unsigned i=0; i<maxwords; i++) {      //create thread      rc = pthread_create(&threads[i], null, processword, (void *)&words[i] );      if (rc){          cout << "error:unable create thread: " << << "," << rc << endl;          exit(-1);       }   }    // free attribute , wait other threads   pthread_attr_destroy(&attr);   for(unsigned i=0; i<maxwords; i++) {      rc = pthread_join(threads[i], &status);      if (rc){          cout << "error:unable join thread: " << << "," << rc << endl;          exit(-1);       }   }    clock_t endtime = clock();    float diff = (((float)endtime - (float)starttime) / 1000000.0f ) * 1000;   cout<<"done in "<< diff << "ms\n";   delete[] threads;   delete model; } 

the clock function documented return processor time used process. if want measure wall time elapsed, it's not right function.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -