Pulse generator Task

I am fairly new to Digital Design in general. I would like to perform a task to be able to see the number of clock cycles between consecutive pulses and the elapsed time between those pulses.
So, the main idea is that this pulse generator has a pulse signal output and the user can decide the frequency of the clock by setting a value of 8 bits to an input signal (cpre), which is in fact a clock prescaler. So, I know beforehand the number of cycles that theoretically will be between output pulses (as I can set the value of this prescaler), but my goal is to find out a simple way to determine if the number of clock cycles between consecutive pulses is the same in reality as it has to be theoretically (so if my pulse generator module works as expected).
So, what is wrong with this code that follows? as I get the same value for initial_time and final_time, for example.

// task for measuring both the number of clock cycles and the time between consecutive pulses 
  task clockCycles;
    input [N-1:0] data;  
    begin
      clockCyclesCounter = 1'b0; 
      initial_time = $realtime; 
      $display("Initial time: %t", initial_time); 
      cpre = data; 
      while ( pulse == 0 ) begin 
        waitClk; // same as @(posedge(clk))
        clockCyclesCounter = clockCyclesCounter + 1'b1; 
      end
      final_time = $realtime; 
      timeBetweenPulses = final_time - initial_time; 
      $display("Final time: %t", final_time); 
      $display("time between pulses: %t", timeBetweenPulses); 
      $display("number of clock cycles: %d", clockCyclesCounter); 
      // waitClk;
    end
  endtask

In reply to Alejofnb96:

If you call this tasks when pulse is 1 or X it will never enter the while() loop. Also the final_time records the first time pulse is 1 on a clock edge, not the time pulse rose to 1. Does it matter?