Still too slow

elsa kerensaelise at hotmail.com
Sat Jan 30 18:08:20 EST 2010


Hello again,

Thanks for the tips r.e random.ranint(). This improved matters
somewhat, however my program is still too slow. If anyone has any
further tips on how to speed it up, they would be much appreciated!

So, I'm calling evolve(L,limit) from the interactive prompt. L is
initally [[100],['NA']]. Ideally limit would be 10^7.

Here is my program:

import random
n=100

def evolve(L,limit):
	global n
	while n<limit:
		evnt = event()
		if evnt!="None":
			ind = chooseInd(L,n)
			action(evnt,L,ind)

def chooseInd(L,n):
	choiceSum=0
	index=0
	choice = random.randint(1,n)
	while choiceSum < choice:
		choiceSum+=L[index][0]
		index +=1
	return (index-1)

def event():
	choice = random.random()
	if choice <= .3:
		event='b'
	elif choice <= .4:
		event ='d'
	elif choice<= .5:
		event = 'm'
	else:
		event = 'None'
	return event

def action(event, L, index):
	global n
	if event == 'b':
		L[index][0]+=1
		n +=1
	elif event == 'd':
		L[index][0]-=1
		n -=1
	elif event == 'm':
		L.append([1,index])
		n +=1


thanks in advance,

Elsa.



More information about the Python-list mailing list