Just thinking if there is a better way to do this.
Currently I have a working function that generates a 5-alphanumeric keycode based on a given index or number.
The problem is it takes too much time generating it. I'm expecting at least 30 Million records and I've tried running it for just a million records and it takes forever.
Does anyone may suggest how to make this code cleaner and faster? Thanks in advance.
import time
def generate_unique(index_id):
BASE = 35; # zero-based
base36 = ['0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z']
idx = [0, 0, 0, 0, 0]
for i in range(0, index_id - 1):
idx[4] = idx[4] + 1
if idx[4] == BASE:
idx[4] = 0
idx[3] = idx[3] + 1
if idx[3] == BASE:
idx[3] = 0
idx[2] = idx[2] + 1
if idx[2] == BASE:
idx[2] = 0
idx[1] = idx[1]+1
if idx[1] == BASE:
idx[1] = 0
idx[0] = idx[0] + 1
return base36[idx[0]] + base36[idx[1]] + base36[idx[2]] + base36[idx[3]] + base36[idx[4]]
t1 = time.process_time()
for i in range(1, 1000000):
generate_unique(i)
t2 = time.process_time()
print(f"Process completed successfully in {t2 - t1} seconds.")