Cython使性能最大化

时间:2012-03-06 12:36:01

标签: c++ python cython

我一直在尝试编写一个图形应用程序来演示大学项目的机器学习,我一直在用Python开发它。因为Python是一种非常慢的语言,所以我一直在寻找加速运行时执行的方法,并且偶然发现了Cython。我对C / C ++还不是很熟悉,但我已经尽可能静态地输入了我的代码(尽管警告说这会降低可读性/灵活性;这不是我目前主要关心的问题。) / p>

然而,我并没有真正注意到这种实现方式与纯Python相比有任何重大改进,我想知道是否有人有任何关于如何加速它的建议。我会非常高兴加速10倍,但我不确定这是多么逼真。

̶I̶̶h̶a̶v̶e̶n̶'̶t̶̶p̶r̶o̶f̶i̶l̶e̶d̶̶m̶y̶̶c̶o̶d̶e̶̶y̶e̶t̶我已经对我的代码进行了描述,结果链接如下。

因为它仍在进行中,所以布局不是很好,但我做了一些简单的功能分组。

可以找到源代码here。代码中最相关的部分发布在下面。

迭代给定的pad的内存:

cdef findBestApproximation(int padindex):
    cdef double last_collision_x
    cdef double last_collision_y
    cdef double last_collision_i_angle
    cdef double last_collision_i_speed
    cdef double last_collision_f_angle
    cdef double last_collision_f_speed
    cdef double x_divergence
    cdef double y_divergenve
    cdef double f_angular_divergence
    cdef double divergence

    printData("FINDING APPROXIMATION FOR PAD %s...\n" % padindex)
    pad = Pads.padlist[padindex]
    memory = pad.memory
    ball = Balls.ball
    if not memory:
        approximation = getPadMidpoint(padindex)
        return approximation

    collision_data = getCollisionData()
    (last_collision_x, last_collision_y, last_collision_i_angle,
     last_collision_i_speed, last_collision_f_angle,
     last_collision_f_speed) = collision_data

    best_approx = 0
    strictness_coef = 1.03

    for memory_tuple in memory:
        (x_miss, y_miss, x_collision, y_collision, _, _, f_angle, _) = memory_tuple.getData()
        (divergence, x_divergence, y_divergence, f_angular_divergence) = calculateDivergence(memory_tuple, collision_data)

        divergence = x_divergence + y_divergence + f_angular_divergence

        approximation = (divergence, x_miss, y_miss)

        printData("\n\nPAD: %s" % padindex)
        printData("\nLAST COLLISION (X) = %s, CONSIDERED CASE (X) = %s" % (last_collision_x, x_collision))
        printData("pos_x DIVERGENCE: %s" % x_divergence)

        printData("\nLAST COLLISION (Y) = %s, CONSIDERED CASE (Y) = %s" % (last_collision_y, y_collision))
        printData("pos_y DIVERGENCE: %s" % y_divergence)

        printData("\nLAST COLLISION (fAngle) = %s, CONSIDERED CASE (fAngle) = %s" % (last_collision_f_angle, f_angle))
        printData("FINAL ANGLE DIVERGENCE: %s" % f_angular_divergence)

        printData("\nTOTAL DIVERGENCE: %s\n\n" % divergence)

        if not best_approx:
            best_approx = approximation
        else:
            (least_divergence, _, _) = best_approx 
            if divergence < least_divergence:
                best_approx = approximation

    (_, pos_x, pos_y) = best_approx
    approximation = (pos_x, pos_y)
    return approximation

计算并将分数归因于存储在记忆垫内存中的特定过去事件:

cdef calculateDivergence(memory_tuple, collision_data):
    cdef double pos_x_dif
    cdef double pos_y_dif
    cdef double i_angle_dif
    cdef double i_speed_dif
    cdef double f_angle_dif
    cdef double f_speed_dif

    cdef double max_x_difference
    cdef double max_y_difference
    cdef double max_angular_difference

    cdef double x_divergence
    cdef double y_divergence
    cdef double f_angular_divergence
    cdef double total_divergence

    (last_collision_x, last_collision_y, last_collision_i_angle,
     last_collision_i_speed, last_collision_f_angle,
     last_collision_f_speed) = collision_data

    (x_miss, y_miss, x_collision, y_collision,
     i_angle, i_speed, f_angle, f_speed      ) = memory_tuple.getData()

    pos_x_dif   = abs(x_collision - last_collision_x)
    pos_y_dif   = abs(y_collision - last_collision_y)
    i_angle_dif = getAngleDifference(i_angle, last_collision_i_angle)
    i_speed_dif = abs(i_speed - last_collision_i_speed)
    f_angle_dif = getAngleDifference(f_angle, last_collision_f_angle)
    f_speed_dif = abs(f_speed - last_collision_f_speed)

    max_x_difference       = window_width
    max_y_difference       = window_height
    max_angular_difference = 180

    x_divergence         = 100 * pos_x_dif   / max_x_difference
    y_divergence         = 100 * pos_y_dif   / max_y_difference
    f_angular_divergence = 100 * f_angle_dif / max_angular_difference

    #Apply weights.
    x_divergence         *= WeightData.current_weight
    y_divergence         *= WeightData.current_weight
    f_angular_divergence *= (1 - WeightData.current_weight)

    total_divergence = x_divergence + y_divergence + f_angular_divergence

    divergence_data = (total_divergence, x_divergence, y_divergence, f_angular_divergence)
    return divergence_data

编辑:Here's分析代码的结果。 DrawSettingsMenu()是最差的之一,但可以忽略(默认情况下不显示设置菜单)。任何“初始化...”功能也可以忽略。

2 个答案:

答案 0 :(得分:5)

您应首先分析您的代码并查看需要优化的内容。 然后你应该尝试尽可能地优化算法。 如果你在Python中识别出一个速度太慢的函数,你可以尝试使用Cython静态输入它,但是你可以通过在C语言中编写它并从Cython中调用它来获得更好的性能。但是,在优化代码之前,请确保代码正常运行,否则您将浪费时间。

答案 1 :(得分:2)

如果您只是想加快速度,可以尝试其他Python实现,如PyPyUnladen Swallow。如果您使用的是旧版本的Python,您可能还需要查看Psyco