使用Python Decimal库加速运算



我正在尝试运行一个类似于谷歌PageRank算法的函数(当然是出于非商业目的)。以下是Python代码;注意,a[0]是这里唯一重要的东西,而a[0]包含n x n矩阵,例如[[0,1,1],[1,0,1],[1,1,0]]。此外,你可以在维基百科上找到我的代码来源:

def GetNodeRanks(a):        # graph, names, size
    numIterations = 10
    adjacencyMatrix = copy.deepcopy(a[0])
    b = [1]*len(adjacencyMatrix)
    tmp = [0]*len(adjacencyMatrix)
    for i in range(numIterations):
        for j in range(len(adjacencyMatrix)):
            tmp[j] = 0
            for k in range(len(adjacencyMatrix)):
                tmp[j] = tmp[j] + adjacencyMatrix[j][k] * b[k]
        norm_sq = 0
        for j in range(len(adjacencyMatrix)):
            norm_sq = norm_sq + tmp[j]*tmp[j]
        norm = math.sqrt(norm_sq)
        for j in range(len(b)):
            b[j] = tmp[j] / norm
    print b
    return b 

当我运行这个实现(在比3 x 3矩阵大得多的矩阵上,n.b.)时,它没有产生足够的精度来计算秩,从而使我能够有效地比较它们。所以我尝试了这个:

from decimal import *
getcontext().prec = 5
def GetNodeRanks(a):        # graph, names, size
    numIterations = 10
    adjacencyMatrix = copy.deepcopy(a[0])
    b = [Decimal(1)]*len(adjacencyMatrix)
    tmp = [Decimal(0)]*len(adjacencyMatrix)
    for i in range(numIterations):
        for j in range(len(adjacencyMatrix)):
            tmp[j] = Decimal(0)
            for k in range(len(adjacencyMatrix)):
                tmp[j] = Decimal(tmp[j] + adjacencyMatrix[j][k] * b[k])
        norm_sq = Decimal(0)
        for j in range(len(adjacencyMatrix)):
            norm_sq = Decimal(norm_sq + tmp[j]*tmp[j])
        norm = Decimal(norm_sq).sqrt
        for j in range(len(b)):
            b[j] = Decimal(tmp[j] / norm)
    print b
    return b 

即使在这种毫无帮助的低精度下,代码也非常慢,在我坐着等待它运行的时间里从未完成运行。以前,代码很快,但不够精确。

有没有一种合理/简单的方法可以让代码同时快速准确地运行?

加速的几个技巧:

  • 优化循环内部的代码
  • 如果可能的话,把所有的东西都移出内环
  • 不要重新计算,已知的,使用变量
  • 不要做不必要的事情,跳过它们
  • 考虑使用列表理解,它通常会快一点
  • 一旦达到可接受的速度就停止优化

浏览您的代码:

from decimal import *
getcontext().prec = 5
def GetNodeRanks(a):        # graph, names, size
    # opt: pass in directly a[0], you do not use the rest
    numIterations = 10
    adjacencyMatrix = copy.deepcopy(a[0])
    #opt: why copy.deepcopy? You do not modify adjacencyMatric
    b = [Decimal(1)]*len(adjacencyMatrix)
    # opt: You often call Decimal(1) and Decimal(0), it takes some time
    # do it only once like
    # dec_zero = Decimal(0)
    # dec_one = Decimal(1)
    # prepare also other, repeatedly used data structures
    # len_adjacencyMatrix = len(adjacencyMatrix)
    # adjacencyMatrix_range = range(len_ajdacencyMatrix)
    # Replace code with pre-calculated variables yourself
    tmp = [Decimal(0)]*len(adjacencyMatrix)
    for i in range(numIterations):
        for j in range(len(adjacencyMatrix)):
            tmp[j] = Decimal(0)
            for k in range(len(adjacencyMatrix)):
                tmp[j] = Decimal(tmp[j] + adjacencyMatrix[j][k] * b[k])
        norm_sq = Decimal(0)
        for j in range(len(adjacencyMatrix)):
            norm_sq = Decimal(norm_sq + tmp[j]*tmp[j])
        norm = Decimal(norm_sq).sqrt #is this correct? I woudl expect .sqrt()
        for j in range(len(b)):
            b[j] = Decimal(tmp[j] / norm)
    print b
    return b 

现在,关于如何在Python中优化列表处理的示例不多了。

使用sum,更改:

        norm_sq = Decimal(0)
        for j in range(len(adjacencyMatrix)):
            norm_sq = Decimal(norm_sq + tmp[j]*tmp[j])

至:

        norm_sq = sum(val*val for val in tmp)

列表理解:

更改:

        for j in range(len(b)):
            b[j] = Decimal(tmp[j] / norm)

更改为:

    b = [Decimal(tmp_itm / norm) for tmp_itm in tmp]

如果你采用这种编码风格,你也可以优化初始循环,并且可能会发现,一些预先计算的变量正在变得过时。

相关内容

  • 没有找到相关文章

最新更新