performance measurements

Each table row shows performance measurements for this C++ g++ program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0002.172.186,584588  0% 1% 0% 100%
5,000,00021.2521.266,584588  0% 1% 1% 100%
50,000,000213.08213.226,588588  0% 1% 1% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read thread-ring benchmark to see what this program should do.

 notes

gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

 thread-ring C++ g++ #2 program source code

/*

 * The Computer Language Benchmarks Game

 * http://benchmarksgame.alioth.debian.org/

 * Contributed by Premysl Hruby

 * convert to C++ by The Anh Tran

 */

#include <pthread.h>

#include <sched.h>

#include <cstdio>

#include <cstdlib>


typedef unsigned int uint;

const uint NUM_THREADS   = 503;
const uint STACK_SIZE   = 16*1024;


int   token;

pthread_mutex_t      mutex   [NUM_THREADS];
pthread_t         threadid[NUM_THREADS];
char            stacks   [NUM_THREADS][STACK_SIZE];


static
void* thread_func( void *num )
{
   size_t thisnode      = reinterpret_cast<size_t>(num);
   int nextnode      = ( thisnode + 1 ) % NUM_THREADS;

   pthread_mutex_t   *mutex_this_node = mutex + thisnode;
   pthread_mutex_t   *mutex_next_node = mutex + nextnode;

   while (true) 
   {
      pthread_mutex_lock( mutex_this_node );

      if ( token > 0 ) 
      {
         token--;
         pthread_mutex_unlock( mutex_next_node );
      }
      else 
      {
          printf( "%d\n", static_cast<int>(thisnode +1) );
          exit(0);
      }
   }

   return 0;
}


int main(int argc, char** args)
{
   if (argc >= 2)
      token = atoi(args[1]);
   else
      token = 1000; // test case


   pthread_attr_t stack_attr;
   pthread_attr_init(&stack_attr);

   for (uint i = 0; i < NUM_THREADS; i++) 
   {
      // init mutex objects

      pthread_mutex_init( &(mutex[i]), 0);
      pthread_mutex_lock( &(mutex[i]) );

      // manual set stack space & stack size for each thread

      // to reduce memory usage

      pthread_attr_setstack( &stack_attr, &(stacks[i]), STACK_SIZE );

      // create thread using specified stackspace

      pthread_create( &(threadid[i]), &stack_attr, &thread_func, reinterpret_cast<void*>(i) );
   }

   // start game

   pthread_mutex_unlock( &(mutex[0]) );

   // wait for result

   pthread_join( threadid[0], 0 );

   return 1;
}

 make, command-line, and program output logs

Sun, 20 Apr 2014 23:13:56 GMT

MAKE:
/usr/bin/g++ -c -pipe -O3 -fomit-frame-pointer -march=native -pthread  threadring.gpp-2.c++ -o threadring.gpp-2.c++.o &&  \
        /usr/bin/g++ threadring.gpp-2.c++.o -o threadring.gpp-2.gpp_run -lboost_system -lpthread 
rm threadring.gpp-2.c++
0.12s to complete and log all make actions

COMMAND LINE:
./threadring.gpp-2.gpp_run 50000000

PROGRAM OUTPUT:
292

Revised BSD license

  Home   Conclusions   License   Play