Is there a way to speed up a sketch using maxsonar lv sensor and pulseIn

I am setting up a sketch that controls the brightness of an LED based on the distance value sampled from a maxsonar lv sensor. Using the PWM signal using pulseIn() seems to generate much less noisy values than using the analog signal using analogRead(). Is that to be expected? The problem I am having with pulseIn, however, is that it seems to run much slower. Is there anything anyone can suggest to make it faster?

const int pwPin = 14;
long pwReading, cm;
int samples = 3;
int highestReading;

float time;
float previousIntensity;
float maxDist;

int led = 9;           // the pin that the LED is attached to

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(pwPin, INPUT);
  Serial.begin(9600);
  time = 0;
  maxDist = 350;
  previousIntensity = 0;
} 

void loop(){
  // print time spent since last loop
  Serial.println(millis()-time);
  time = millis(); 
  
  // sample sensor value
  highestReading = 0;
  for(int i = 0; i < samples ; i++)
  {
    pwReading = pulseIn(pwPin, HIGH);
    // get the highest value
    if(pwReading > highestReading)
      highestReading = pwReading;
  }
  // convert to centimeters
  cm = highestReading/57.87;

  // shape the curve of the result
  float intensity = 1-constrain(float(cm),20,maxDist)/maxDist;  
  intensity = intensity*intensity*intensity;
  intensity = previousIntensity*.75 + intensity*.25;
  intensity = constrain(intensity,.005,1);
  previousIntensity = intensity;

  // set the brightness of pin 9:
  float val= 200*intensity;
  analogWrite(led, val);
}

cm = highestReading/57.87;

division is expensive

cm = highestReading * (1/57.87); // you can precalculate

for(int i = 0; i < samples ; i++)

use uint8_t iso int as that is incremented faster

there is a lot of float math that can be replaced by integer math if you e.g. multiply the values by 1000 and do the math + apply correction factors in the end.

  maxdist = 350; 
 // convert to centimeters
  cm = highestReading/57.87;

  // shape the curve of the result
  float intensity = 1-constrain(float(cm),20,maxDist)/maxDist;  
  intensity = intensity*intensity*intensity;
  intensity = previousIntensity*.75 + intensity*.25;
  intensity = constrain(intensity,.005,1);
  previousIntensity = intensity;

  // set the brightness of pin 9:
  float val= 200*intensity;
  analogWrite(led, val);

==>

  unsigned long maxDist = 350000;  // * 1000

  unsigned long d = highestReading * 17.28;  // * 1000
  // do constrain manually
  if (d < 20000) d = 20000;
  else if (d > maxDist) d = MaxDist;  

  d = d / (maxDist/1000);  // d = 60 ... 1000  (approx)

  unsigned long intensity = 1000 - d;  // intensity = 0..940

  intensity = intensity * intensity * intensity;  // intensity = approx  0.. 850000000  
  intensity = (previousIntensity * 3 + intensity)/4;
  previousIntensity = intensity;

  // map to value 0..255
  int val = intensity / 3333333;
 
  analogWrite(led, val);

the numbers are wrong (need tuning) but you should get the idea

another idea is to use a lookup table or multimap - Arduino Playground - MultiMap -