Optimizing code on arduino due

The first step in optimizing the code is to start over. Before you do that, though. Read up on arrays.

The second step is to COMPLETELY ditch the String class. There is no excuse for being so lazy.

There is NO excuse for having 5 functions to turn a pin on and 5 more to turn a pin off. That can be done with ONE function that takes a pin number and a state. There are not 54 functions to read the 54 digital pins on a Due. Don't write your code that way.

  if (prezs == HIGH){
    prezs = 1;
  }else{
    prezs = 0;
  }

That's stupid. If the value is 1 (that's the value associated with the name HIGH), make it 1. Otherwise, make the value 0 (which is what it already was).

  if (String((char)payload[0]) == "R") {

Completely asinine.

   if(payload[0] == 'R')
   {

You got most of the rest of them right (except the unnecessary cast). Why did you f**k that one (and a few others) up?

1 - From time to time rpms and rpmd are getting "inf" value for couple of seconds and then get back to normal. Why is that happen ?

What is 1/0? It's undefined, but generally accepted to be infinity.

2 - Also in the actual version of the code, if I stop the wheel suddenly at 100 rpm, then the value that arduino sends for rpm don t get to 0 but is still sending that 100 rpm value. How can I fix that?

When you stop the wheel, interrupts stop happening. You need to pay attention to how long it has been since an interrupt happened. Which means that in the ISR is the wrong place to be calculating RPM. ALL that the ISR should do is increment a counter.

float times0, times01, times2=0, rpms=0;
float timed0, timed01, timed2=0, rpmd=0;

Times are NOT floats.