CODE optimization for speed

Hi everyone,
How can I improve the performance for this code?
I need it to be as fast as possible with as less delay as possible.

#define NUM_SENSORS 6
#define EMITTER_PIN 3 // set emitter pin for PWM control of brightness
#define threshold 350  // threshold between black line and white surface, verify it first
int setPointValue = (NUM_SENSORS - 1)*500; // subtract from position for error, depends on LED brightness
int position = setPointValue; // readLine value when black line is in the middle (verify it first)
int prev_position = position; // stores previous position so robot remembers the last position before it lost the black line
void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
readLine();
}

void readLine() // get the position of the sensors relative to line, for PID based line following
{
  long int sum = 0;
  long int weighted_sum = 0;
  int val[NUM_SENSORS]; // stores the values read from IR sensor
  int cal_val[NUM_SENSORS]; // stores calibrated values, either 0 or 1000
  // 0 indicates white surface, 1000 indicates black line
  
  for (int i=0; i<NUM_SENSORS; i++) 
  {
  cal_val[i]=0;
  val[i] = analogRead(i); // reads values from sensor
    if (val[i]>=threshold) { // might need to be changed, depending how the resistor work
      cal_val[i]=0;         // sensor value becomes 0 when val[i] is >=threshold, indicates white surface
    } else {
      cal_val[i]=1000;      //sensor value becomes 1000 when val[i] is <=threshold, indicates black surface
    } // END IF
    weighted_sum=(((cal_val[i])*i*1000)+weighted_sum); // computes weighted sum of sensor values
    sum=cal_val[i]+sum; // computes sum of sensor values
  } // END FOR LOOP
  position=weighted_sum/sum; // computes position
       //position=(sum of weighted values multiplied by 1000)/sum
  if (position == -1) { // use previous position if current one is error-causing
    // -1 indicates dividing by zero, which happens when line is lost
    position=prev_position;
  } else { // otherwise just proceed as usual with value storage
    prev_position=position;
  } // END IF
 } // END readLine

Thanks in advance

Edit your post and change quote for code where you see that keyword within (see the italic you have above? That comes from [i] in your code in your for loop that gets interpreted on display as an instruction to go to italic text)

notfreeusername:
I need it to be as fast as possible with as less delay as possible.

Division is slow. Avoid it if you can. One way is to have the number of samples as 2, 4 , 8 or 16 (i.e. a power of 2) and then you can do the division using right shift >>

It seems to me that your use of 1000 and threshold are duplications. Why not just use the threshold so you don't need all those tests to convert to 1000 (or to 0).

More fundamentally, how long does your function take - measure it. Maybe the slowness is elsewhere in the code you have not posted.

...R