S
stormslayer
Folks:
I've been considering a shift to python. I currently use c++builder
(borland) or perl. I do computational models / statistics
programming, and was interested in python b/c it
a. has a library that connects to the R stats package
b. has code that seems way more readable than anything else
There is, however, at least for my work, a hard constraint. Execution
time for large data sets / lots of iterations of a model needs to be
reasonabe. So, I wrote a program to test it out (appended to the
bottom of this email). As I said, this is my first python program.
I'm sure it is terrible, but in many ways, you hope that the
interpreter / compiler corrects some errors (or the langauge isn't
that easy to use after all).
Benchmarks (for the paramter settings in the file):
C++builder 6.0: 3 seconds
Perl (ActivePerl 5.6.1.638): 14 seconds
Python (ActivePython 2.3.2 Build 232): 1 minute 13 seconds
Note that I didn't fiddle overmuch with any of the programs -- each
one took about 15 minutes to bang out on the keyboard. I'm hoping
that there is some obvious mistake in the way I used something in
python to account for the speed differential. It seems like a really,
really nice language, but orders of magnitude slower than C/C++ and a
5x slowdown over Perl seems abusive...
Thanks for any advice.
sd
Python code:
#!/usr/bin/env python
import random
pop=[] # popluation of agents; initially empty list
N = 10000 # population size
ep = .05 # mutation rate
its = 100 # number of times through the main loop
gold=0 # initial number of gold adopters; (1-gold) = silver
adopters
standard=0 # either 1 for gold or 2 for silver
shifts=0 # number of regime shifts
def init_all(): # init globals
global pop, gold, standard
pop = []
gold = 0
for i in range(N):
if random.randint(1,2) == 1:
pop.append(1)
gold=gold+1
else: pop.append(2)
if gold>N/2: standard=1
else: standard=2 # if tie, silver wins
# print "Initial Population of Gold Users: ", gold
# end function init_all
def one_choose():
global pop, standard, gold, shifts
for i in range(its):
temp = random.randint(0,N-1)
tempval = pop[temp]
old_stand = standard
if random.random()<ep:
if random.random()<0.5:
pop[temp]=1
if tempval!=pop[temp]:
gold=gold+1
if gold>N/2: standard=1
else:
pop[temp]=2
if tempval!=pop[temp]:
gold=gold-1
if gold<N/2: standard=2
if standard!=old_stand: shifts=shifts+1 # check for regime
shift after each agent chooses anew
else:
if gold>N/2:
if pop[temp]!=1:
pop[temp]=1
gold=gold+1
if gold>N/2: standard=1
else:
if pop[temp]!=2:
pop[temp]=2
gold=gold-1
if gold<N/2: standard=2
if standard!=old_stand: shifts=shifts+1 # check for regime
shift after each agent chooses anew
# print "Final Population of Gold Users: ", gold
# end function one_choose
# start main loop
for i in range(1000):
init_all()
one_choose()
print "Number of regime shifts: ", shifts
I've been considering a shift to python. I currently use c++builder
(borland) or perl. I do computational models / statistics
programming, and was interested in python b/c it
a. has a library that connects to the R stats package
b. has code that seems way more readable than anything else
There is, however, at least for my work, a hard constraint. Execution
time for large data sets / lots of iterations of a model needs to be
reasonabe. So, I wrote a program to test it out (appended to the
bottom of this email). As I said, this is my first python program.
I'm sure it is terrible, but in many ways, you hope that the
interpreter / compiler corrects some errors (or the langauge isn't
that easy to use after all).
Benchmarks (for the paramter settings in the file):
C++builder 6.0: 3 seconds
Perl (ActivePerl 5.6.1.638): 14 seconds
Python (ActivePython 2.3.2 Build 232): 1 minute 13 seconds
Note that I didn't fiddle overmuch with any of the programs -- each
one took about 15 minutes to bang out on the keyboard. I'm hoping
that there is some obvious mistake in the way I used something in
python to account for the speed differential. It seems like a really,
really nice language, but orders of magnitude slower than C/C++ and a
5x slowdown over Perl seems abusive...
Thanks for any advice.
sd
Python code:
#!/usr/bin/env python
import random
pop=[] # popluation of agents; initially empty list
N = 10000 # population size
ep = .05 # mutation rate
its = 100 # number of times through the main loop
gold=0 # initial number of gold adopters; (1-gold) = silver
adopters
standard=0 # either 1 for gold or 2 for silver
shifts=0 # number of regime shifts
def init_all(): # init globals
global pop, gold, standard
pop = []
gold = 0
for i in range(N):
if random.randint(1,2) == 1:
pop.append(1)
gold=gold+1
else: pop.append(2)
if gold>N/2: standard=1
else: standard=2 # if tie, silver wins
# print "Initial Population of Gold Users: ", gold
# end function init_all
def one_choose():
global pop, standard, gold, shifts
for i in range(its):
temp = random.randint(0,N-1)
tempval = pop[temp]
old_stand = standard
if random.random()<ep:
if random.random()<0.5:
pop[temp]=1
if tempval!=pop[temp]:
gold=gold+1
if gold>N/2: standard=1
else:
pop[temp]=2
if tempval!=pop[temp]:
gold=gold-1
if gold<N/2: standard=2
if standard!=old_stand: shifts=shifts+1 # check for regime
shift after each agent chooses anew
else:
if gold>N/2:
if pop[temp]!=1:
pop[temp]=1
gold=gold+1
if gold>N/2: standard=1
else:
if pop[temp]!=2:
pop[temp]=2
gold=gold-1
if gold<N/2: standard=2
if standard!=old_stand: shifts=shifts+1 # check for regime
shift after each agent chooses anew
# print "Final Population of Gold Users: ", gold
# end function one_choose
# start main loop
for i in range(1000):
init_all()
one_choose()
print "Number of regime shifts: ", shifts