PrintTime
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:
- Timestamp Storage: System.currentTimeMillis() returns current time in milliseconds, written as string via ReadWrite.writeString()
- Elapsed Calculation: ReadWrite.readString() reads stored timestamp, Long.parseLong() converts to long, arithmetic subtraction calculates elapsed milliseconds
- Output Format: Tools.format("%.2f", elapsed/1000d) converts milliseconds to seconds with 2 decimal places precision
- File Handling: File.exists() checks existence, ReadWrite methods handle all file operations, overwriting on each execution
Memory Usage
Low-overhead utility with constrained memory requirements:
- JVM Memory: Fixed at 8MB (-Xmx8m)
- File I/O: Reads and writes only a single timestamp string
- Processing: Simple arithmetic operations on long integers
Output Behavior
The tool provides conditional output streams based on argument parsing and file state:
- No Arguments: System.err.println("Time:\t"+millis) outputs current timestamp to stderr only
- New File: No elapsed time output, ReadWrite.writeString() creates timestamp file silently
- Existing File: System.out.println() and System.err.println() both output "Elapsed:\t" + formatted time
- Time Format: Tools.format("%.2f", elapsed/1000d) produces seconds with 2 decimal places (e.g., "45.23")
- Optional Suppression: Parse.parseBoolean(args[1]) controls output when second argument provided
Use Cases
- Script Performance: Measure execution time of bioinformatics pipelines
- Workflow Timing: Track time between processing steps
- Benchmarking: Compare performance across different runs
- Process Monitoring: Simple timing integration in shell scripts
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
- Creation: File is created automatically if it doesn't exist
- Updates: File is overwritten with new timestamp on each call
- Permissions: Requires read/write access to the specified file location
- Cleanup: Timestamp files can be safely deleted when timing is complete
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
- Use descriptive names:
alignment.timer
,assembly.timer
- Include process IDs for parallel jobs:
job_${PID}.timer
- Organize in temp directories:
/tmp/timers/process.timer
Support
For questions and support:
- Email: bbushnell@lbl.gov
- Documentation: bbmap.org