[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] fun game



On 4/23/07, James Paige <Bob@xxxxxxxxxxxxxxxxxxx> wrote:
I am working on a really fun game, but I am having a hard time making it
more fun. I need some help. Here is the source code so far:

#------------------------------------
print "Preparing to do fun things..."
import math
import random

print "Commencing fun..."
while True:
  math.sqrt(random.random())
#------------------------------------

The trouble is, when I check my processor usage on my dual processor Mac
G4, I only have one processor averaging around 90% fun, and sometimes
dropping as low as 86% fun. Does anybody know a more fun operation than
math.sqrt that I could use to push the fun up to 100%? Is there any way
I am going to be able to get both processors up to 100% without using
multithreading? I figure I should be able to max out at 200% fun on this
system, but I am stuck here. Any advice?

James, I often see new PyGame users run into this issue when trying to maximize fun. The problem really has been exacerbated with the introduction of mult-processor and multi-core consumer machines, where the fun tends to stick to one processor. Fortunately, the solution is simple. For maximum fun-ness, you need to be using threads.

Since you can't guarantee which processor a thread will be assigned
to, it's best to launch N*10 threads, where N is the numbers of
logical processors you have.

Unfortunately, pure python threads don't seem to want to take up more
than ~140% fun on my dual-core system.  I think the issue is that
python doesn't really thread as much as it should unless you call out
to a C library.  Perhaps someone with more experience than I could
enlighten me.

Nevertheless, 140% fun is better than 86% fun, so I've included my code below:

(please note that though I implemented nice thread monitoring and
reporting of the end of fun, the fun that we're having does not end
until killed manually)

#!/usr/bin/env python
#
# Maximizing fun-ness

from threading import Thread
import math, random, time

#------------------------------------------------------------------------------
# Thread Class

class FunnessThread(Thread):

  def __init__ (self):
     Thread.__init__(self)

  def run(self):
     while True:
       math.sqrt(random.random())

#------------------------------------------------------------------------------
# Launching threads for maximum fun-ness

N = 2   # N is the number of processors in your machine
threadlist = []
for i in range(N*10):
  curr_thread = FunnessThread()
  threadlist.append(curr_thread)
  curr_thread.start()

# Wait for all threads to finish
index = 0
donelist = []
sleeptime = 1
while threadlist:
  t = threadlist[index]
  if t.isAlive():
     index += 1
  else:
     donelist.append(t)
     threadlist.remove(t)
     print "'%s' is no longer having fun" % t
  if index >= len(threadlist):
     index = 0
     print "%d threads left having fun." % len(threadlist)
     if threadlist:
        print "Not done having fun.  Sleeping for %f seconds..." % sleeptime
        time.sleep(sleeptime)

print "All done having fun"