[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] fun game
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] fun game
- From: Nathan <nathan.stocks@xxxxxxxxx>
- Date: Mon, 23 Apr 2007 17:45:37 -0600
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Mon, 23 Apr 2007 19:46:03 -0400
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=covaeLHLkUXL2QUDUKom0Vpsc9g5w7r3297vR+asqRAkZSEmBs+lQUOzNeXmuijqcEY+ut5dVaTIeR5TgtqRWnO1f+RuMjLbsHvXMhBTAMBGlLnnXeVw7gOWFOxfv7ik+2x5xPMgnNyCyELANW0Pw7f1oIRqtenAV93WPJsePGo=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=rgUCbNtSFsz39sExsyrPtpV7PkHl9LIQW6UpBXaElRUYBAIWld/8YYRLUCb8X20TFHfc69SGyxuujDCVz36/jYps5wkhIDwba+B2CYAUvOZqupAzcxND8/y9RwjTdqiYoot9Uz2xbN3F15eBIwxm2vE67wb8nijEYtrHQ2KpaIE=
- In-reply-to: <20070423224508.GE12140@karnov.dreamhost.com>
- References: <20070423224508.GE12140@karnov.dreamhost.com>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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"