Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I rewrite my expensive pygame functions in C?

Is it possible for me to find the expensive functions(for example, A* pathfinding) in my pygame game and rewrite them as extensions as outlined here?

Is there a speed benefit to doing so? Is there a better (python) solution?

I ask this question because I have just started learning C for unrelated reasons, when it occured to me this might be a good idea when I go back to Python and pygame.

like image 897
talloaktrees Avatar asked Dec 16 '22 23:12

talloaktrees


1 Answers

Is there a speed benefit to doing so?

It's impossible to say without knowing what it is exactly that you're doing, but the general answer is "very likely".

Is there a better (python) solution?

Again, it is impossible to say. Better than what exactly?

If you are working with numerical arrays, then the first step should probably be to use NumPy.

Once you've done that, there are multiple avenues for speeding things up other than coding extensions in raw C:

  • Cython: write extensions in a Python-like language;
  • numexpr: fast evaluation engine for array expressions.

Finally, if you do find yourself writing C or C++ extensions, consider using SWIG or Boost.Python.

like image 183
NPE Avatar answered Dec 25 '22 22:12

NPE