Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: A silly question about C

Author: Álvaro Begué

Date: 16:12:36 07/20/04

Go up one level in this thread


By keeping several different max_value variables, you can break the dependency
chain between iterations of the loop.


int find_max_original(int const *values){
  int max_value = 0, i;

  for (i=0; i<= 99; i++) {
    if (values[i] > max_value) max_value = values[i];
  }

  return max_value;
}

int find_max_faster(int const *values){
  int max_value_0=values[0];
  int max_value_1=values[1];
  int max_value_2=values[2];
  int max_value_3=values[3];
  int const *end = values+100;
  for(values+=4;values!=end;values+=4){
    if(max_value_0<values[0])
      max_value_0=values[0];
    if(max_value_1<values[1])
      max_value_1=values[1];
    if(max_value_2<values[2])
      max_value_2=values[2];
    if(max_value_3<values[3])
      max_value_3=values[3];
  }
  if(max_value_0<max_value_1)
    max_value_0=max_value_1;
  if(max_value_2<max_value_3)
    max_value_2=max_value_3;
  if(max_value_0<max_value_2)
    max_value_0=max_value_2;

  return max_value_0;
}


find_max_faster() is more than twice as fast as find_max_original(). I'm using
gcc 3.4.0 with options -O3 and -mpentium4.

Can other people, please, test it with other compilers?




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.