pythonperformancefreebasic

python performance compared to freebasic


I found that my code of python is really slow, using 8min to finish compared to 1min to freebasic
could anyone please suggest some optimization

first to create digit_array["a","b","c","d","e","f","g","h","i","j"].
divide it, get remainder, again and again, store it into array_index[] , 1 as the last digit.
else if and else code to deal with the last iteration of not complete division.
store maximum index as variable i.
then loop array_index from the highest value to 1, map it to digit_array,
then generate a string(code commented)

added: 20120903
I make this to create string with letters combination from digit_array, so I can skip some not common letters like 'q' 'z' or some digits like '4'
currpos can make me start from last time
x make me adjust total letters in digit_array, can be a-z+A-Z+0-9+' '
currpos_end 1024th will give,
a result 1024 if digit_array is ['0','1','2','3','4','5','6','7','8','9'] , x = 10
a result bace if digit_array is ['a','b','c','d','e','f','g','h','i','j'] , x = 10
a result 1467 if digit_array is ['0','1','2','3','4','5','6','7','8'] , x = 9
a result 2000 if digit_array is ['0','1','2','3','4','5','6','7'] , x = 8
now I can print results out already, only slow in performance

python 3 code,

    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

freebasic code,

    Dim digit_array(0 To 100) As String
    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"

    Dim array_index(0 To 50) As ULongInt
    For i As Integer = 0 To 50
        array_index(i) = 0
    Next

    Dim As ULongInt currpos
    Dim As ULongInt currpos_end
    Dim As ULongInt currpos2
    Dim As ULongInt x = 10 'ubound digit_array + 1, ("0","1") binary then x = 2
    Dim As ULongInt i

    currpos = 0
    currpos_end = (26+10+1)^5
    'currpos_end = 1024
    Print currpos
    currpos2 = currpos
    Print

    For currpos = currpos To currpos_end

        currpos2 = currpos
        i = 1

        Do
            If currpos2 > x Then
                array_index(i) = currpos2 Mod x
                currpos2 = CULngInt(currpos2 / x)
            ElseIf currpos2 = x Then
                array_index(i) = 0
                i += 1
                array_index(i) = 1
                Exit Do
            Else
                array_index(i) = currpos2
                Exit Do
            EndIf
            i += 1
        Loop

        'Print i; " . "; currpos; " "; currpos2

        '''first = i Until 1 = last
        For j As Integer = i To 1 Step -1
            'Print digit_array(array_index(j));
        Next
        'Print

        'Sleep
    Next

    Print "."
    Sleep

Solution

  • thx How 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