A lightweight and extremely easy-to-use performance timer for measuring code execution time between different timestamps. Just call stamp() method to automatically calculate the duration between adjacent timestamps.
Author: Peaceful-World-X License: MIT Version: 1.0.0
- ✅ Extremely Simple API - Only one
stamp()method needed - ✅ Automatic Calculation - Auto calculates duration between adjacent timestamps
- ✅ Real-time Output - Immediate feedback with each call
- ✅ Flexible Labels - Optional custom labels or auto-generated labels
- ✅ Detailed Reports - Generate comprehensive reports with percentages
- ✅ Multiple Timers - Support multiple independent timers simultaneously
- ✅ Reset Functionality - Reset and reuse timers
from performance_timer import SimpleTimer
# Create timer
T = SimpleTimer()
T.stamp() # Start timing
# ... your code ...
T.stamp() # Auto display duration for first segment
# ... more code ...
T.stamp() # Auto display duration for second segment
T.report() # View complete reportT = SimpleTimer("MyTask")
T.stamp() # Start timing
# ... database query ...
T.stamp("database_query") # Output: [MyTask] database_query: 0.1234s
# ... data processing ...
T.stamp("data_processing") # Output: [MyTask] data_processing: 0.0567s
# ... result output ...
T.stamp("result_output") # Output: [MyTask] result_output: 0.0123s
T.report() # Generate detailed report[MyTask] Timer started...
[MyTask] database_query: 0.1203s
[MyTask] data_processing: 0.0803s
[MyTask] result_output: 0.0301s
=== MyTask Performance Report ===
Total segments: 3
Total time: 0.2307s
--------------------------------------------------
1. database_query 0.1203s ( 52.1%)
2. data_processing 0.0803s ( 34.8%)
3. result_output 0.0301s ( 13.1%)
SimpleTimer(name: str = "SimpleTimer")Record timestamp and calculate duration from previous timestamp
- Args:
label- Optional label to identify this time segment - Returns: Duration in seconds from previous timestamp, None for first timestamp
- Output: Automatically prints timing information
Generate and display detailed performance report
- Returns: Report string
Reset timer and clear all data
Get list of all recorded durations
- Returns: Copy of durations list
Get total time of all segments
- Returns: Total time in seconds
T = SimpleTimer("PerformanceAnalysis")
T.stamp()
expensive_algorithm_a()
T.stamp("algorithm_A")
expensive_algorithm_b()
T.stamp("algorithm_B")
T.report() # Compare algorithm performanceT = SimpleTimer("APIProcessing")
T.stamp()
validate_request()
T.stamp("request_validation")
query_database()
T.stamp("database_query")
process_business_logic()
T.stamp("business_logic")
T.report()T = SimpleTimer("DataPipeline")
T.stamp()
load_data()
T.stamp("data_loading")
clean_data()
T.stamp("data_cleaning")
transform_data()
T.stamp("data_transformation")
T.report()- Use meaningful labels:
T.stamp("database_query")is better thanT.stamp() - Create separate timers for different tasks: Avoid confusion between different scenarios
- Check reports regularly: Use
T.report()for complete performance overview - Reset and reuse: Use
T.reset()to clear data before starting new measurements
python performance_timer.pyThis will run a comprehensive demonstration showing all features of SimpleTimer.
一个轻量级且极其简单易用的Python时间戳计时器,用于测量不同时间戳之间的代码执行时间。只需调用 stamp() 方法就能自动计算相邻时间戳之间的耗时。
作者: 薛文耀 许可证: MIT 版本: 1.0.0
- ✅ 极简API - 只需要一个
stamp()方法 - ✅ 自动计算 - 自动计算相邻时间戳之间的耗时
- ✅ 实时输出 - 每次调用立即显示耗时
- ✅ 灵活标签 - 支持自定义标签或自动生成标签
- ✅ 详细报告 - 生成包含百分比的详细报告
- ✅ 多计时器 - 支持同时使用多个独立的计时器
- ✅ 重置功能 - 可以重置计时器重新开始
from performance_timer import SimpleTimer
# 创建计时器
T = SimpleTimer()
T.stamp() # 开始计时
# ... 执行代码 ...
T.stamp() # 自动显示第一段耗时
# ... 执行更多代码 ...
T.stamp() # 自动显示第二段耗时
T.report() # 查看完整报告T = SimpleTimer("我的任务")
T.stamp() # 开始计时
# ... 数据库查询 ...
T.stamp("数据库查询") # 输出: [我的任务] 数据库查询: 0.1234s
# ... 数据处理 ...
T.stamp("数据处理") # 输出: [我的任务] 数据处理: 0.0567s
# ... 结果输出 ...
T.stamp("结果输出") # 输出: [我的任务] 结果输出: 0.0123s
T.report() # 生成详细报告[我的任务] Timer started...
[我的任务] 数据库查询: 0.1203s
[我的任务] 数据处理: 0.0803s
[我的任务] 结果输出: 0.0301s
=== 我的任务 Performance Report ===
Total segments: 3
Total time: 0.2307s
--------------------------------------------------
1. 数据库查询 0.1203s ( 52.1%)
2. 数据处理 0.0803s ( 34.8%)
3. 结果输出 0.0301s ( 13.1%)
SimpleTimer(name: str = "SimpleTimer")stamp(label: Optional[str] = None) -> Optional[float]
- 记录时间戳并计算与上一个时间戳的差值
- 参数:
label- 可选的标签,用于标识这个时间段 - 返回: 与上一个时间戳的时间差(秒),如果是第一个时间戳则返回None
- 输出: 自动打印耗时信息
report() -> str
- 生成并打印详细的性能报告
- 返回: 报告字符串
reset()
- 重置计时器,清除所有数据
get_durations() -> List[float]
- 获取所有时间段的耗时列表
- 返回: 耗时列表的副本
get_total_time() -> float
- 获取总耗时
- 返回: 所有时间段的总耗时
T = SimpleTimer("性能分析")
T.stamp()
expensive_algorithm_a()
T.stamp("算法A")
expensive_algorithm_b()
T.stamp("算法B")
T.report() # 比较两个算法的性能T = SimpleTimer("API处理")
T.stamp()
validate_request()
T.stamp("请求验证")
query_database()
T.stamp("数据库查询")
process_business_logic()
T.stamp("业务逻辑")
T.report()T = SimpleTimer("数据处理流水线")
T.stamp()
load_data()
T.stamp("数据加载")
clean_data()
T.stamp("数据清洗")
transform_data()
T.stamp("数据转换")
T.report()- 使用有意义的标签:
T.stamp("数据库查询")比T.stamp()更好 - 为不同任务创建不同计时器: 避免混淆不同的测试场景
- 及时查看报告: 使用
T.report()获得完整的性能概览 - 重置后重用: 使用
T.reset()清理数据后重新开始
python performance_timer.py这将运行一个全面的演示,展示SimpleTimer的所有功能。
This project is licensed under the MIT License - see the LICENSE file for details.
Peaceful-World-X【耗不尽的先生】
SimpleTimer makes performance timing as simple as taking timestamps! Perfect for developers who need quick and intuitive performance measurements.
SimpleTimer 让性能计时变得像打时间戳一样简单!非常适合需要快速直观性能测量的开发者。