Hallo all,

I am still very new to C. I wrote a program that basically does evaluations on all binary numbers between 0 and 2^27. When aggressively compiled, the program is already quite fast (takes 40 sec on my 64bit , 2core MacBook). Would multithreading significantly improve the execution speed in a program which basically does through all binary numbers? If yes, is <pthread.h> the library to use? Here it is:

Code:`#include <bitset>`

#include <iostream>

#include <math.h>

int main()

{

static const unsigned n = 27;

static char *c[ n ] = {"aut","bel","bul","cyp","cze","deu","dnk","esp","est","fin","fra","gbr","grc","hun","irl","ita","ltu","lux","lva","mlt","nld","pol","prt","rom","svk","svn","swe"};

static unsigned int w[ n ] = {10,12,10,4,12,29,7,27,4,7,29,29,12,12,7,29,7,4,4,3,13,27,12,14,7,4,10};

static float p[ n ] = {8.34,10.59,7.6,0.8,10.32,82.18,5.47,45.27,1.34,5.31,63.92,61.09,11.18,10.03,4.39,59.21,3.37,0.48,2.27,0.41,16.39,38.09,10.67,21.45,5.4,2.02,9.22};

static const unsigned nc = 1 << n;

double pr = 0.0f;

unsigned int sumv = 0;

unsigned int sumw = 0;

float sump = 0.0f;

double COLEMAN[ 2 ] = {0.0};

double BANZHAF[ n ][ 2 ] = {{0.0}};

bool win_ni = false;

bool win_lb = false;

unsigned int i;

for ( i = nc; i--; )

{

std::bitset < n > v = i;

sumv = v.count();

if ( sumv < 15 ) continue;

sumw = 0;

sump = 0;

pr = pow( 0.5 , sumv ) * pow( 0.5 , n - sumv );

win_ni = false;

win_lb = false;

unsigned int k;

for ( k = n; k--; ) { if ( v[ n - k - 1 ] ) { sumw += w[ k ]; sump += p[ k ]; } }

if ( sumv >= 18 && sumw >= 255 && sump >= 308.01 ) { COLEMAN[ 0 ] += pr; win_ni = true; }

if ( sump >= 322.92 || sumv >= 24 ) { COLEMAN[ 1 ] += pr; win_lb = true; }

if(win_ni || win_lb)

{

unsigned int j;

for ( j = n ; j--; )

{

v.flip( j );

sumv = v.count();

sumw = 0;

sump = 0;

k = 0;

for ( k = n; k--; ) { if ( v[ n - k - 1 ] ) { sumw += w[ k ]; sump += p[ k ]; } }

if ( win_ni && ( sumv < 18 || sumw < 255 || sump < 308.01 ) ) BANZHAF[ j ][ 0 ] += pr;

if ( win_lb && ( ( sumv < 15 || sump < 322.92 ) && sumv < 24 ) ) BANZHAF[ j ][ 1 ] += pr;

v.flip( j );

}

}

}

printf( "Coleman ; Efficiency\n" );

printf( "Nice 27 ; Lisbon 27\n" );

printf( "%f ; %f\n" , COLEMAN[ 0 ], COLEMAN[ 1 ] );

printf( "Banzhaf ; Voting ; Powers\n" );

printf( "State ; Nice 27 ; Lisbon 27\n" );

i = 0;

for ( i = n; i--; ) { printf(" %s ; %f ; %f\n" , c[ n - i - 1 ], BANZHAF[ i ][ 0 ], BANZHAF[ i ][ 1 ] ); }

return 0;

}