Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing code

Author: Gerd Isenberg

Date: 02:12:51 07/08/03

Go up one level in this thread


On July 08, 2003 at 04:16:45, Gerd Isenberg wrote:

>On July 07, 2003 at 17:40:46, Russell Reagan wrote:
>
>>This implements Tim's method. I get the output n = 1799993753, time = 6.339 on
>>Athlon 2GHz. Let's implement some of the others and compare.
>>
>
>Hi Russell,
>
>hmm there is something wrong - my first try of your code:
>
>n = 179992636, time = 0.687
>
>aha, with
>#define MAX_ITERATIONS 100000000
>
>i get on P4 2.4GHz
>
>n = 1799993753, time = 6.875
>
>I bet that in this "dumb" test loops the switch256 approach is fastest...
>But i have to work a bit, so maybe later...
>
>Regars,
>Gerd

dump switch256 test:

n = 1799993753, time = 2.484

but of course that's nonsence and "unfair" due to all the multiple funci()
are simply combined together....

eg.
void func6 () { n += 7; }
void func7 () { n += 8; }
is
void func6_7 () { n += 15; }

if i avoid this with:

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; }
...
    stop = clock();
    n = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;

i got

n = 1799993753, time = 2.671

some assembler snippets of the switch:

0040102D 48                   dec         eax
0040102E 3D FE 00 00 00       cmp         eax,0FEh
00401033 0F 87 02 0F 00 00    ja          00401F3B
00401039 FF 24 85 BC 1F 40 00 jmp         dword ptr [eax*4+401FBCh]
// case 1:
00401040 FF 05 DC CF 40 00    inc         dword ptr ds:[40CFDCh]
00401046 E9 F0 0E 00 00       jmp         00401F3B
...
// end of switch
00401F3B 46                   inc         esi
00401F3C 81 FE 00 E1 F5 05    cmp         esi,5F5E100h
00401F42 0F 8C DA F0 FF FF    jl          00401022

so the whole switch is only
0x00401F3B - 0x00401039 = 0x0F02 = 3842 Byte < 4KByte

Cheers,
Gerd


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;
        switch (bits) {
		case 0x00:
			break;
		case 0x01:
			func0();
			break;
		case 0x02:
			func1();
			break;
		case 0x03:
			func0();
			func1();
			break;
		case 0x04:
			func2();
			break;
		case 0x05:
			func0();
			func2();
			break;
		case 0x06:
			func1();
			func2();
			break;
		case 0x07:
			func0();
			func1();
			func2();
			break;
		case 0x08:
			func3();
			break;
		case 0x09:
			func0();
			func3();
			break;
		case 0x0a:
			func1();
			func3();
			break;
		case 0x0b:
			func0();
			func1();
			func3();
			break;
		case 0x0c:
			func2();
			func3();
			break;
		case 0x0d:
			func0();
			func2();
			func3();
			break;
		case 0x0e:
			func1();
			func2();
			func3();
			break;
		case 0x0f:
			func0();
			func1();
			func2();
			func3();
			break;
...




This page took 0 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.