Discussion:
[pygame] Pygame to C/C++?
Michael
2004-03-01 04:08:24 UTC
Permalink
I'm thinking of rewriting some of my Python/Pygame code into C (or C++?)
with SDL to get a speed improvement. I haven't written anything in C/C++
in a long time but think I could pick it back up. Does anyone have any
tutorials of rewriting that kind of thing?
Pete Shinners
2004-03-01 04:21:12 UTC
Permalink
Post by Michael
I'm thinking of rewriting some of my Python/Pygame code into C (or C++?)
with SDL to get a speed improvement. I haven't written anything in C/C++
in a long time but think I could pick it back up. Does anyone have any
tutorials of rewriting that kind of thing?
Good luck on this. There hasn't been a lot of experimentation on this sort
of thing. I'm sure other besides myself are interested in seeing what kind
of results you will get.

Pygame functions have an almost 1-to-1 mapping to SDL functions, so porting
shouldn't be too confusing. Pygame also comes with a C-api so you can get at
the main "glue" code from the C end. It's not really well documented, but
you can look at some of the pygame source itself for reference.
Derek Simkowiak
2004-03-01 05:38:27 UTC
Permalink
Post by Pete Shinners
Post by Michael
I'm thinking of rewriting some of my Python/Pygame code into C (or
C++?) with SDL to get a speed improvement.
[...]
Post by Pete Shinners
Good luck on this. There hasn't been a lot of experimentation on this
sort of thing. I'm sure other besides myself are interested in seeing
what kind of results you will get.
I've done this. It worked f'ing sweet for me.

Tips:
1. Never trust your instinct on where to optimize -- it's mostly wrong. Use the Python profiler to find what to rewrite.

2. Use SWIG.

Note that moving to SDL will only speed things up if the slow part is in Python. Moving to C does not reduce the number of calculations that must be done in a game -- it just drops all the overhead that comes with Python bytecode.


--Derek
Bob Ippolito
2004-03-01 05:50:45 UTC
Permalink
Post by Derek Simkowiak
Post by Pete Shinners
Post by Michael
I'm thinking of rewriting some of my Python/Pygame code into C (or
C++?) with SDL to get a speed improvement.
[...]
Post by Pete Shinners
Good luck on this. There hasn't been a lot of experimentation on this
sort of thing. I'm sure other besides myself are interested in seeing
what kind of results you will get.
I've done this. It worked f'ing sweet for me.
1. Never trust your instinct on where to optimize -- it's mostly
wrong. Use the Python profiler to find what to rewrite.
2. Use SWIG.
Note that moving to SDL will only speed things up if the slow part is
in Python. Moving to C does not reduce the number of calculations
that must be done in a game -- it just drops all the overhead that
comes with Python bytecode.
From my experience, I would say use Pyrex or Boost (if C++).. not SWIG.
I totally agree with your other points, though.

-bob

Bob Ippolito
2004-03-01 05:46:53 UTC
Permalink
Post by Pete Shinners
Post by Michael
I'm thinking of rewriting some of my Python/Pygame code into C (or
C++?) with SDL to get a speed improvement. I haven't written anything
in C/C++ in a long time but think I could pick it back up. Does
anyone have any tutorials of rewriting that kind of thing?
Good luck on this. There hasn't been a lot of experimentation on this
sort of thing. I'm sure other besides myself are interested in seeing
what kind of results you will get.
Pygame functions have an almost 1-to-1 mapping to SDL functions, so
porting shouldn't be too confusing. Pygame also comes with a C-api so
you can get at the main "glue" code from the C end. It's not really
well documented, but you can look at some of the pygame source itself
for reference.
Makes it sound like a good place to put Pyrex in the mix..

-bob
Dmitry Borisov
2004-03-01 04:37:44 UTC
Permalink
Have you profiled your code to see what the bottleneck is ?
If you see Python problem, you could use psyco. Sometimes it gives up to
50x.
If you have blitting problem, you may work on optimizing your surfaces so
blits go faster.
If you use Numeric to have some cool features, try to write your own C
module instead.
Personally I don't think using C++ will make any difference if you don't
know where's the problem is.
Anyway, good luck.
Dmitry/

Subject: [pygame] Pygame to C/C++?
Post by Michael
I'm thinking of rewriting some of my Python/Pygame code into C (or C++?)
with SDL to get a speed improvement. I haven't written anything in C/C++
in a long time but think I could pick it back up. Does anyone have any
tutorials of rewriting that kind of thing?
Continue reading on narkive:
Loading...