比较bmat和hs的性能速度

2024-09-19 22:40:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图比较bmat和hstack在连接巨大的坐标矩阵时的速度

我的代码:

from scipy.sparse import bmat, coo_matrix, hstack
import timeit

import numpy as np

a = np.add(*np.indices((1000,1000)))

A = coo_matrix(a)
B = coo_matrix(a)

t = timeit.timeit("hstack([A,B]).toarray()", "from __main__ import A, B, hstack", number= 2000)
print(t)

tt = timeit.timeit("bmat([[A,B]])", "from __main__ import A, B, bmat", number = 2000)
print(tt)

从我的实验来看,bmat似乎比hstack有更快的处理速度

85.89499111475607
71.0423545148451

我想知道我的测试方法是否正确,或者在coo_矩阵庞大的情况下,是否有任何东西可以让hstack表现得更好?我应该使用bmat还是hstack,从实验来看,bmat似乎更适合使用,但我想知道两者的区别


Tags: 代码fromimportnumbermainnp矩阵matrix