PrintTime

Script: printtime.sh Package: align2 Class: PrintTime.java

Prints time elapsed since last called on the same file. A simple timing utility for measuring intervals between executions.

Basic Usage

printtime.sh <filename>

The tool requires a filename as an argument. This file is used to store timestamp information between calls.

Parameters

This tool uses positional arguments rather than named parameters:

filename
Required. The file used to store timestamp data. If the file exists, the tool calculates elapsed time since the timestamp stored in the file. If the file doesn't exist, it will be created with the current timestamp.
print_output
Optional second argument (boolean). Controls whether elapsed time is printed to stdout and stderr. Default is true. This parameter is handled internally by the Java code.

Examples

Basic Timing

# First call - creates timestamp file
printtime.sh timing.log

# Later call - shows elapsed time
printtime.sh timing.log
# Output: Elapsed: 45.23

Creates or updates a timestamp file and shows elapsed time in seconds since the last call.

Timing Script Execution

# Start timing
printtime.sh script_timer.log

# Run your commands here
some_long_running_command.sh

# Check elapsed time
printtime.sh script_timer.log
# Output: Elapsed: 120.45

Useful for measuring execution time of scripts or processes.

Multiple Timing Points

# Initialize timing
printtime.sh process.timer

# Step 1
process_data.sh
printtime.sh process.timer
# Output: Elapsed: 30.12

# Step 2
analyze_data.sh  
printtime.sh process.timer
# Output: Elapsed: 15.67

Track timing at multiple checkpoints in a workflow.

Algorithm Details

Implementation Strategy

The PrintTime utility implements a simple file-based timing mechanism using System.currentTimeMillis() and file I/O operations:

Memory Usage

Low-overhead utility with constrained memory requirements:

Output Behavior

The tool provides conditional output streams based on argument parsing and file state:

Use Cases

File Operations

Timestamp File Format

The timestamp file contains a single line with the timestamp in milliseconds since Unix epoch:

# Example timestamp file contents
1642785234567

File Management

Error Handling

Common issues and their solutions:

File Permission Errors
Ensure the user has read/write permissions for the timestamp file location. The tool will fail silently if it cannot write to the specified file.
Invalid Timestamp
If the timestamp file contains invalid data (non-numeric), Long.parseLong() throws NumberFormatException. Delete the corrupted file and restart timing.
No Arguments
When called without arguments, the tool prints the current timestamp to stderr but does not perform elapsed time calculations.

Integration Tips

Shell Script Integration

#!/bin/bash
# Initialize timing
printtime.sh pipeline.timer

# Your pipeline steps
step1.sh
echo "Step 1 completed in $(printtime.sh step1.timer | grep Elapsed | cut -f2) seconds"

step2.sh  
echo "Step 2 completed in $(printtime.sh step2.timer | grep Elapsed | cut -f2) seconds"

echo "Total pipeline time: $(printtime.sh pipeline.timer | grep Elapsed | cut -f2) seconds"

Log File Naming

Support

For questions and support: