python性能与freebasi的比较

2024-09-30 02:16:38 发布

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

我发现我的python代码非常慢,只需要8分钟就可以完成,而freebasic只需要1分钟 有人能给我一些优化建议吗

首先创建数字数组[“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”,“i”,“j”]。
将其除以,得到余数,一次又一次地将其存储到array_index[],1作为最后一位。
else if和else代码来处理未完成除法的最后一次迭代。
将最大索引存储为变量i。
然后将数组索引从最高值循环到1,将其映射到数字数组中,
然后生成一个字符串(代码注释)

新增:20120903
我这样做是为了从digit_数组中创建带有字母组合的字符串,这样我就可以跳过一些不常见的字母,比如'q''z'或者一些数字,比如'4'
currpos可以让我从上次开始
x让我调整数字数组中的字母总数,可以是a-z+a-z+0-9+''
currpos_end 1024将给出,
如果数字数组是['0','1','2','3','4','5','6','7','8','9',x=10
如果数字数组是['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j']、x=10
如果数字数组是['0','1','2','3','4','5','6','7','8',x=9
如果数字数组是['0'、'1'、'2'、'3'、'4'、'5'、'6'、'7']、x=8
现在我已经可以打印出结果了,只是性能比较慢

python 3代码

    digit_array=[]
    for i in range(0, 10): # range(10) ----> 0 1 2 3 4 ... 9
        digit_array.append("")
    digit_array[0] = "a"
    digit_array[1] = "b"
    digit_array[2] = "c"
    digit_array[3] = "d"
    digit_array[4] = "e"
    digit_array[5] = "f"
    digit_array[6] = "g"
    digit_array[7] = "h"
    digit_array[8] = "i"
    digit_array[9] = "j"

    array_index=[]
    for i in range(0, 51):
        array_index.append(0)

    currpos = 0
    currpos_end = pow((26+10+1),5)
    #currpos_end = 1024
    print (currpos)
    currpos2 = currpos
    x = 10 # ubound digit_array + 1, ("0","1") binary then x = 2
    print ()

    for currpos in range(currpos, currpos_end):

        currpos2 = currpos
        i = 1

        while True :
            if currpos2 > x:
                array_index[i] = currpos2 % x
                currpos2 = int(currpos2 / x)
            elif currpos2 == x:
                array_index[i] = 0
                i += 1
                array_index[i] = 1
                break
            else:
                array_index[i] = currpos2
                break
            i += 1

        print (i, " . ", currpos, currpos2)

        for j in range(i , 1, -1):
        #    break   #uncomment this and comment next 3 lines for speed testing  
            print (digit_array[array_index[j]], end='')
        print (digit_array[array_index[1]], end='')
        print ()

    try:
        input(".")
    except:
        pass

自由基代码

^{pr2}$

Tags: 代码inforindexrange数字数组array
2条回答

thxHow to convert an integer in any base to a string?

def calculation():
    while 1:
        global s, n
        r = n % base
        if r == 0: return
        s = digits[r] + s
        n = n // base
        if n == 0: return

#                  .          .          .
digits = "X0123456789"
digits = "X23456789AJKQ" #first digit not use, remain for calculation
currpos = 0
currpos = 30941 #22222 base13 13^4+13^3+13^2+13+1
#currpos_end = 371292 #QQQQQ base13 13^5-1
currpos_end = pow((26+10+1),5)
currpos_end = 371292
#currpos_end = 1024
base = len(digits)
print (currpos)
print (base)
print ()

for currpos in range(currpos, currpos_end):
    n = currpos
    s = ""
    calculation()
    if s == "":
        continue
    print (s, " . ", currpos, " . ", n)
    #do something here with s

为了好玩,我试着弄清楚你想在代码里做什么,但还是放弃了。在

不管怎样,这里有一些代码被清理了一下:

digit_array=[
             "a",
             "b",
             "c",
             "d",
             "e",
             "f",
             "g",
             "h",
             "i",
             "j"
             ]

array_index = [0] * 51 

#currpos_end = pow((26+10+1),5)
currpos_end = 1024

x = 10 # ubound digit_array + 1, ("0","1") binary then x = 2
for currpos in range(currpos_end):
    i = 1
    while True :
        if currpos > x:
            array_index[i] = currpos % x
            currpos = int(currpos / x)

        elif currpos == x:
            array_index[i] = 0
            i += 1
            array_index[i] = 1
            break

        else:
            array_index[i] = currpos
            break
        i += 1

result = "".join([digit_array[i] for i in array_index])
print(result)

这将产生以下结果:

^{pr2}$

相关问题 更多 >

    热门问题