限制Cython CPU和RAM使用率?

时间:2020-03-22 08:45:56

标签: python performance cython

最近,我安装了Cython并使用它来加快python程序的速度(素数测试)。当我给出一个较小的数字时,它可以正常工作并且运行更快。但是,由于提供了如此庞大的数量,我的计算机开始冻结,因为CPU和RAM都达到100%。

是否可以限制Cython CPU和RAM的使用?

这是我的代码(使用Cython语言):

from math import *

cpdef trial_factoring(float M, float p, list bits_list):
    cdef float t
    t = sqrt((M+1)*2)

    cdef float k_range
    k_range = (t-1)//(2*p)

    cdef float k
    k = 1 

    cdef list potential_filter
    potential_filter = []                   

    cdef float potential

    while k <= k_range:
        potential = 2*k*p + 1
        if potential % 8 == 3 or potential % 8 == 5:
            pass
        else:
            potential_filter.append(potential)
        k += 1

    cdef int a
    a = 1   

    cdef int factor
    cdef int i

    for factor in potential_filter:
        for i in range(len(bits_list)):
            a *= a                  
            if bits_list[i] == 1:        
                a *= 2              
                a %= factor         

        if a == 1:                  
            return False            

    return True                     

0 个答案:

没有答案