时间测量
时间测量工具模块。
提供算法运行时间测量的多种方式。
- class pynetim.timing.AlgorithmTimer(algorithm_instance)[source]
Bases:
object算法运行时间测量器。
用于测量算法类实例的运行时间,支持多次运行统计。
- 支持的算法类型:
启发式算法(DegreeDiscount, SingleDiscount等):只需 k 参数
贪婪算法(Greedy, CELF):需要 k, mc_rounds 等参数
RIS算法(IMM, TIM, OPIM):需要 k 参数,部分需要 num_rr_sets
Example
>>> from pynetim.timing import AlgorithmTimer >>> from pynetim import DegreeDiscountAlgorithm >>> >>> # 创建算法实例 >>> algorithm = DegreeDiscountAlgorithm(graph) >>> >>> # 创建计时器 >>> timer = AlgorithmTimer(algorithm) >>> >>> # 单次运行(启发式算法) >>> seeds, runtime = timer.run(k=10) >>> >>> # 单次运行(贪婪算法) >>> seeds, runtime = timer.run(k=10, mc_rounds=1000, random_seed=42) >>> >>> # 多次运行统计 >>> stats = timer.run_multiple(k=10, num_runs=5) >>> print(f"Mean runtime: {stats['mean']:.4f}s")
- property last_runtime: float | None
获取最后一次运行的运行时间。
- Returns:
运行时间(秒),如果没有运行过则返回 None。
- Return type:
Optional[float]
- run(**kwargs)[source]
运行算法并测量时间。
- Parameters:
**kwargs –
传递给算法 run() 方法的参数。 必须包含 k 参数(种子节点数量)。
常用参数: - k: 种子节点数量(必填) - mc_rounds: 蒙特卡洛模拟次数(贪婪算法) - random_seed: 随机数种子 - use_multithread: 是否启用多线程 - num_rr_sets: RR集合数量(RIS算法)
- Returns:
(种子节点集合, 运行时间)
- Return type:
Example
>>> # 启发式算法 >>> seeds, runtime = timer.run(k=10)
>>> # 贪婪算法 >>> seeds, runtime = timer.run(k=10, mc_rounds=1000, random_seed=42)
>>> # OPIM 算法 >>> seeds, runtime = timer.run(k=10, num_rr_sets=1000)
- run_multiple(num_runs=10, **kwargs)[source]
多次运行算法并统计运行时间。
- Parameters:
num_runs (int) – 运行次数,默认 10。
**kwargs – 传递给算法 run() 方法的参数。 必须包含 k 参数(种子节点数量)。
- Returns:
- 包含以下统计量:
mean: 平均运行时间
std: 标准差
min: 最小运行时间
max: 最大运行时间
median: 中位数
total: 总运行时间
- Return type:
Example
>>> stats = timer.run_multiple(k=10, num_runs=5) >>> print(f"Mean: {stats['mean']:.4f}s, Std: {stats['std']:.4f}s")
- pynetim.timing.compare_algorithms_runtime(algorithms, **kwargs)[source]
比较多个算法的运行时间。
- Parameters:
- Returns:
- 每个算法的运行结果和时间。
格式:{算法名: {‘seeds’: 种子集合, ‘runtime’: 运行时间}}
- Return type:
Example
>>> from pynetim.timing import compare_algorithms_runtime >>> from pynetim import DegreeDiscountAlgorithm, CELFAlgorithm >>> >>> algorithms = { ... 'degree_discount': DegreeDiscountAlgorithm(graph), ... 'celf': CELFAlgorithm(graph) ... } >>> results = compare_algorithms_runtime(algorithms, k=10) >>> for name, data in results.items(): ... print(f"{name}: {data['runtime']:.4f}s, {len(data['seeds'])} seeds")
- pynetim.timing.measure_runtime(func, *args, **kwargs)[source]
测量函数运行时间。
- Parameters:
func (Callable) – 要测量的函数。
*args – 函数位置参数。
**kwargs – 函数关键字参数。
- Returns:
(函数返回值, 运行时间秒数)
- Return type:
Example
>>> from pynetim.timing import measure_runtime >>> result, runtime = measure_runtime(algorithm.run, k=10) >>> print(f"Runtime: {runtime:.4f}s")
- pynetim.timing.measure_runtime_multiple_runs(func, num_runs=10, *args, **kwargs)[source]
多次运行函数并统计运行时间。
- Parameters:
- Returns:
- 包含以下统计量:
mean: 平均运行时间
std: 标准差
min: 最小运行时间
max: 最大运行时间
median: 中位数
total: 总运行时间
- Return type:
Example
>>> from pynetim.timing import measure_runtime_multiple_runs >>> stats = measure_runtime_multiple_runs(algorithm.run, num_runs=5, k=10) >>> print(f"Mean runtime: {stats['mean']:.4f}s")
- pynetim.timing.measure_time(func)[source]
装饰器:测量函数或方法的运行时间。
可以用于类方法或普通函数,支持C++绑定方法。
- Parameters:
func (Callable) – 要测量的函数或方法。
- Returns:
包装后的函数。
- Return type:
Callable
Example
>>> from pynetim.timing import measure_time >>> >>> @measure_time >>> def my_algorithm(graph, k): ... # 算法实现 ... return seeds >>> >>> seeds, runtime = my_algorithm(graph, k=10) >>> print(f"Runtime: {runtime:.4f}s")
或者用于类方法: >>> class MyAlgorithm: … @measure_time … def run(self, k): … return self._find_seeds(k)
Note
装饰器会修改函数的返回值,将原始返回值和运行时间打包成元组返回。 对于类方法,装饰器会自动处理 self 参数。