8.3. Time Functions: $time vs $realtime
Accurate time measurement is essential for performance analysis and delay verification.
8.3.1 Understanding Timescale
Before discussing time functions, understand the `timescale` directive:
Verilog
`timescale <time_unit>/<time_precision>
// Example: `timescale 1ns/1ps
// time_unit = 1ns (used in delays like #10 means 10ns)
// time_precision = 1ps (simulator resolution)
8.3.2 $time - Integer Time Value
Returns: 64-bit integer representing current simulation time
Characteristic: Rounded DOWN to the time unit (truncates precision)
Verilog
`timescale 1ns/1ps
module time_integer_demo;
initial begin
$display("Time Unit: 1ns, Precision: 1ps");
$display("----------------------------------");
#10 $display("After #10 : $time = %0t ns", $time); // 10
#5.5 $display("After #5.5 : $time = %0t ns", $time); // 15 (truncated)
#7.893 $display("After #7.893: $time = %0t ns", $time); // 22 (truncated)
end
endmodule
Output:
Verilog
After #10 : $time = 10 ns
After #5.5 : $time = 15 ns
After #7.893: $time = 22 ns
8.3.3 $realtime - Real Number Time Value
Returns: Real number with full precision
Characteristic: Maintains fractional part based on time precision
Verilog
`timescale 1ns/1ps
module realtime_precision_demo;
real precise_time;
initial begin
$display("Comparing $time vs $realtime:");
$display("-------------------------------");
#10.567;
$display("$time = %0t (integer, truncated)", $time);
$display("$realtime = %0t (real, preserved)", $realtime);
$display("$realtime = %.3f ns (float format)", $realtime);
precise_time = $realtime;
$display("Stored in real variable: %.6f ns", precise_time);
end
endmodule
Output:
Verilog
$time = 10 (integer, truncated)
$realtime = 10.567 (real, preserved)
$realtime = 10.567 ns (float format)
Stored in real variable: 10.567000 ns
8.3.4 Practical Use Case: Performance Measurement
Verilog
`timescale 1ns/1ps
module performance_analyzer;
real start_time, end_time, duration;
integer operations, i;
reg [31:0] data;
initial begin
operations = 1000;
// Start measurement
start_time = $realtime;
$display("Starting %0d operations at time %.3f ns", operations, start_time);
// Simulate operations
for (i = 0; i < operations; i = i + 1) begin
data = data + i;
#0.125; // Each operation takes 0.125ns
end
// End measurement
end_time = $realtime;
duration = end_time - start_time;
$display("Completed at time %.3f ns", end_time);
$display("Total duration: %.3f ns", duration);
$display("Average time per operation: %.6f ns", duration / operations);
$display("Throughput: %.2f ops/ns", operations / duration);
end
endmodule
Output:
Verilog
Starting 1000 operations at time 0.000 ns
Completed at time 125.000 ns
Total duration: 125.000 ns
Average time per operation: 0.125000 ns
Throughput: 8.00 ops/ns
8.3.5 Comparison Summary

