Skip to main content

structiming

结构化、可预测的 Python 计时与性能观测工具

  • ✅ 零依赖
  • ✅ JSON / 字符串双输出
  • ✅ 单次计时 / 循环统计
  • ✅ logging 完全可控

一、核心概念(非常重要)

✅ Stimer

观测「单次 / 少量执行」

  • 累计总耗时
  • 记录执行次数
  • 支持中途 mark

✅ StimerLoop

观测「同一逻辑被执行 N 次的耗时分布」

  • 总耗时 / 均值 / 最小 / 最大
  • 对每个 mark 也做统计分析
  • 三种等价用法

二、Stimer 用法

✅ 1️⃣ with 语句(推荐)

执行后默认立即输出(可修改log参数)

with Stimer("train", epoch=1) as t:
    t.mark("start")
    work()
    t.mark("done")
    work()

✅ 2️⃣ 装饰器

执行后默认立即输出(可修改log参数)

@Stimer("train", epoch=1)
def train():
    t = all_stimers["train"]
    work()
    t.mark("half")
    work()

✅ 3️⃣ 手动 start / stop

执行后不立即输出

t = Stimer("train", epoch=1)
t.start()
work()
t.mark("done")
work()
t.stop()

✅ 输出示例(JSON)

{
  "name": "train",
  "time_unit": "ms",
  "total": 1234.567,
  "count": 1,
  "marks": {
    "start": 12.345,
    "done": 678.901
  },
  "epoch": 1
}

三、StimerLoop 用法(✅ 三种等价)

StimerLoop 只有一种生命周期模型:

with StimerLoop(...):
    for _ in loop.iter():
        ...

三种用法只是不同入口。


✅ 用法一(推荐)

执行后默认立即输出(可修改log参数)

with StimerLoop("train", number=5) as loop:
    for _ in loop.iter():
        work()
        loop.mark("epoch_end")

✅ 用法二:装饰器

执行后默认立即输出(可修改log参数)

@StimerLoop("train", number=5)
def train():
    t = all_stimers["train"]
    work()
    t.mark("epoch_end")

✅ 用法三:func + run

不能mark,只能统计耗时 执行后不立即输出

loop = StimerLoop("train", number=5, func=work)
loop.run()

✅ 三种方式统计结果基本一致


✅ 输出示例(JSON)

{
  "name": "train",
  "time_unit": "ms",
  "number": 5,
  "total": 5432.100,
  "min": 1023.456,
  "max": 1123.789,
  "mean": 1086.420,
  "marks": {
    "epoch_end": {
      "mean": 512.345,
      "min": 498.123,
      "max": 534.678
    },
    "epoch_end2": {
      "mean": 987.654,
      "min": 965.432,
      "max": 1001.234
    }
  }
}

四、logging 行为说明(✅ 非常重要)

log 参数 行为
None ✅ 完全不输出
未传 ✅ 输出到控制台(structiming)
logging.Logger ✅ 输出到你指定的 logger
Stimer("train", log=None)     # 静默
Stimer("train")               # 控制台
Stimer("train", log=mylogger)

五、all_stimers(全局访问)

t = all_stimers["train"]
print(t.export_json())
print(t.export_str())

Metadata

Release files for structiming 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for structiming 0.1.0
File Size Uploaded Provenance Actions
structiming-0.1.0.tar.gz 4.7 kB Without provenance Details for structiming-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for structiming 0.1.0
File Interpreter ABI Platform Actions
structiming-0.1.0-py3-none-any.whl Python 3 none any Details for structiming-0.1.0-py3-none-any.whl

Total release size: 11.0 kB

Release files / structiming-0.1.0.tar.gz

Download URL structiming-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bd41d6f4c0bb7b30474ff9324e2c803176e28093297c458bf7c5ff5bae7340ee
BLAKE2b-256 checksum
How to use checksums
7a999cc8b72e90c6e9c7bd2b8bd64c8e2b3c4d84291d9c6bb041a3a7000dc369
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.9

Release files / structiming-0.1.0-py3-none-any.whl

Download URL structiming-0.1.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19ea982b6df7e2904a7401820121fd25569d6daeb45d264648ebb66848bebf72
BLAKE2b-256 checksum
How to use checksums
eef03bac533f671e095d64303f569baee8bb24a54711fb02875089dfa9bc890e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.9

Security for structiming 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page