Measure Unrar Execution Time in a Script

Measuring the execution time of an unrar extraction command is essential for benchmarking file decompression performance, identifying system bottlenecks, and optimizing automated backup workflows. This guide covers how to track unrar execution duration precisely across different scripting environments, including Linux Bash scripts, Windows PowerShell, and cross-platform Python scripts.

1. Using Bash (Linux / macOS)

In Bash, you can measure execution time using built-in timestamp calculation or the time utility.

Method A: Nanosecond Calculation with date

This approach captures timestamps immediately before and after the extraction command, making it easy to store the result in a variable for logging.

#!/usr/bin/env bash

archive="archive.rar"
destination="./extracted/"

# Capture start time in nanoseconds
start_time=$(date +%s%N)

# Execute the extraction
unrar x -inul "$archive" "$destination"

# Capture end time in nanoseconds
end_time=$(date +%s%N)

# Calculate duration in milliseconds
elapsed_ms=$(( (end_time - start_time) / 1000000 ))

echo "Extraction completed in ${elapsed_ms} ms."

Note: The -inul flag suppresses standard unrar messages to keep logs clean.

Method B: The GNU time Utility

To measure CPU usage alongside wall-clock time without modifying script logic heavily, use the standalone /usr/bin/time binary:

/usr/bin/time -f "Elapsed: %E (Real), User: %U, Sys: %S" unrar x -inul archive.rar

2. Using Windows PowerShell

Windows environments typically use PowerShell's Measure-Command cmdlet or [System.Diagnostics.Stopwatch].

Method A: Measure-Command

Measure-Command executes a script block and outputs an object detailing the duration.

$archive = "archive.rar"
$destination = "C:\Extracted\"

$duration = Measure-Command {
    Start-Process -FilePath "unrar.exe" -ArgumentList "x -inul `"$archive`" `"$destination`"" -Wait -NoNewWindow
}

Write-Output "Extraction took $($duration.TotalSeconds) seconds."

Method B: .NET Stopwatch

For higher precision and easier logging within complex scripts:

$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

& unrar.exe x -inul archive.rar C:\Extracted\

$stopwatch.Stop()
Write-Output "Elapsed Time: $($stopwatch.Elapsed.TotalMilliseconds) ms"

3. Using Python

If you manage extractions in a platform-independent workflow, use Python's time.perf_counter() alongside subprocess:

import subprocess
import time

archive = "archive.rar"
destination = "./extracted/"

start_time = time.perf_counter()

result = subprocess.run(
    ["unrar", "x", "-inul", archive, destination],
    capture_output=True,
    text=True
)

end_time = time.perf_counter()
elapsed = end_time - start_time

if result.returncode == 0:
    print(f"Unrar finished successfully in {elapsed:.4f} seconds.")
else:
    print(f"Extraction failed: {result.stderr}")