On Nov 24, 2010, at 12:40 PM, Kris Schnee wrote:
> On 2010.11.24 1:53 PM, Nathan BIAGINI wrote:
>> Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
>> didn't know there is a way to manage keyboard state without the common
>> event loop.
>> I ll use this created topic to ask something else about the optimization
>> of a game written with pygame. Restrict frames per second with
>> clock.tick(xx) and only update dirty rects (in case of static
>> background) is enough for a quite "simple" 2D game? Or there re more
>> optimizations to do? I don't try to reach perfect performance, only want
>> to make my faster as possible and less CPU consuming.
> 
> One other thing to try is an acceleration system.
> "Psyco" uses a compiler to speed up a Python program very easily. See 
> http://psyco.sourceforge.net/ . All you need to do after installing it is add 
> the code "import psyco ; psyco.full()".
> 
> "Boost" ( http://beta.boost.org/doc/libs/1_45_0/libs/python/doc/index.html ) 
> lets you write code in the C language and access that from Python. I've not 
> tried that.

There is a saying widely attributed to Don Knuth, "Premature optimization is 
the root of all evil."

The Wikipedia article on Optimization explains well: 
http://en.wikipedia.org/wiki/Program_optimization

The general theory is:
1) Pick an efficient algorithm to start off with. In your case, only drawing 
the changed stuff might be a good choice, but it might depend on your graphics 
card. It might be just as fast to draw everything.

2) Do not "optimize" in the traditional sense of, for instance, rewriting 
clear, obvious code as "faster" but obscure stuff. This is for three reasons:
2a) You might optimize the wrong piece of code -- Kernighan and Plaugher's 
"Elements of Programming Style" explains a case like this.

2b) Your "optimization" might not actually help, or might even make things 
worse, if the compiler is smart. "Do not write C code in Python." 
2c) Irrespective of 2a and 2b, you WILL end up with messy, hard to read code 
that is more likely to have bugs. Python's main advantage over C is that it can 
be easier to read and understand. Don't throw that away to get a tiny speedup! 
(But see step 4, below.)

3) Run the code, and see if it is actually slow. If not, you're done. THIS IS 
THE MOST IMPORTANT STEP.

4) If it is slow, do measurements to figure out WHERE it is slow. Then look at 
that part, and figure out if there's a faster algorithm to use in that place. 
For instance, you might find that you're using a lot of objects in a loop, 
creating a lot of garbage to be collected. Switching your approach to one that 
re-uses objects could fix that -- but it might also make the code hard to read, 
so you don't want to do that unless you absolutely have to. In any case, save 
copies so you can switch back.

5) If everything is slow, you might get "for free" speedup using psyco. But 
that won't help, for instance, if your code mostly runs fine, but occasionally 
slows down due to garbage collection.


All that being said, I know it's easy to put a frame rate counter on your code, 
and then get excited about making the number bigger. If you're just learning to 
code, BY ALL MEANS hack away! Just try to learn from your hacks, especially 
based on the advice above. And definitely spend a little time learning how to 
use Subversion, Git, or Mercurial, so you can revert your changes quickly.


Reply via email to