Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing code - Andrews one

Author: Gerd Isenberg

Date: 02:31:04 07/08/03

Go up one level in this thread


Andrew's one - as expected faster than Tim's approach

so far:
Andrew    n = 1799993753, time = 5.828
Tim       n = 1799993753, time = 6.875
Switch256 n = 1799993753, time = 2.671

so now i really have to work ;-)

Cheers,
Gerd


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_ITERATIONS 100000000

unsigned long n = 0;
unsigned long n1 = 0;
unsigned long n2 = 0;
unsigned long n3 = 0;
unsigned long n4 = 0;
unsigned long n5 = 0;
unsigned long n6 = 0;
unsigned long n7 = 0;
unsigned long n8 = 0;

// Just some functions to call
void func0 () { n1 += 1; }
void func1 () { n2 += 2; }
void func2 () { n3 += 3; }
void func3 () { n4 += 4; }
void func4 () { n5 += 5; }
void func5 () { n6 += 6; }
void func6 () { n7 += 7; }
void func7 () { n8 += 8; }

void tim_func () {
    int i;
    clock_t start, stop;

    srand(0);
    start = clock();
    for (i = 0; i < MAX_ITERATIONS; i++) {
        unsigned long bits = rand() & 0xff;
		if ( bits & 0xc0 )
		{
			if ( bits & 0x80 ) func7();
			if ( bits & 0x40 ) func6();
			if ((bits &~0xc0) == 0 ) goto done;
		}
		if ( bits & 0x30 )
		{
			if ( bits & 0x20 ) func5();
			if ( bits & 0x10 ) func4();
			if ((bits &~0x30) == 0 ) goto done;
		}
		if ( bits & 0x0c )
		{
			if ( bits & 0x08 ) func3();
			if ( bits & 0x04 ) func2();
		}
		if ( bits & 0x03 )
		{
			if ( bits & 0x02 ) func1();
			if ( bits & 0x01 ) func0();
		}
		done:;
    }
    stop = clock();
	n = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
    printf("n = %u, time = %.3f\n", n, (float)(stop - start) / CLOCKS_PER_SEC);
}

void main (void) {
    tim_func();
}




This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.