tips for reading digital signals

Hello, I am creating a code for reading digital signals that should operate as follows:
at each logical state change in the digital port the arduino must terminate one count of time and start another, in this way I get the logical levels and their respective widths. the code works well for widths over 100ms, but shows an inaccurate reading for smaller widths. I want to use the code for widths of 500us. anyone have an idea to optimize the code and allow such readings accurately?

#define sensor 3


bool state = 0;
bool previousState = 0;


unsigned long t0 , largura = 0;




void setup() {
 
pinMode(sensor,INPUT);

Serial.begin(9600);

}

void loop() {
  state = digitalRead(sensor);
 
  

     if(state!= previousState)
       {
        largura = (micros() - t0);
        t0 = micros();
  Serial.println(largura);
        
        Serial.println(state);
        previousState = state;

       }


    

}

How about using direct port manipulation to read the digital input to speed it up ?

Do you need to print the data on every state change or could you save it in an array and print in batches ? How about using a faster baud rate when printing ?

What type of sensor are you using ?
Do you have a pullup or pulldown resistor on the input ?

port manipulation should Works better but i`m
I have no ability with this low level.

i dont need the data in real time,
actually i want save the state and width in arrays, the source is
a 433mhz RF receiver.
I'm not using pull up resistor but I do not know the RF receiver circuit

the Arduino(Atmega328p, 16Mhz) has enough speed to measure so small pulse widths?

port manipulation should Works better but i`m
I have no ability with this low level.

Port manipulation at the level you want is easy
Try this

void setup()
{
  Serial.begin(115200);
  pinMode(3, INPUT_PULLUP);
}

void loop()
{
  Serial.println(bitRead(PIND, 3));
  delay(500);
}

Wire the circuit to take pin 3 LOW when activated

Measuring a pulse width and then doing something with the data are two completely different things. Heed Uncle Bob and get as close to the metal as possible. The faster you can get your reads and writes out of the way, the more time for other things. There's also a way to get rid of the overhead of Serial as well (if you want to get down and dirty).

Yes, every task take some clock pulses(time) , so with the clean code can arduino handle this smal pulse widths?
Should i use interrupts?

For 500us, you don't need interrupts although they have some benefits that may outweigh the hair you are going to lose while learning to use interrupts.

thanks a lot for the tips, after the implementation in the code the accuracy of the reading has increased a lot. now I will continue the code saving the data in arrays.