When does it make sense to rewrite the Python module in C?

In the game I am writing, I use the class of 2D vectors that I wrote to handle the speeds of objects. This is called a large number of times each frame, since there are many objects on the screen, so any increase that I can make at its speed will be useful.

It is quite simple, consisting mainly of wrappers for related mathematical functions. It would be pretty trivial to rewrite in C, but I'm not sure if this will make any significant difference, since all it really does is call basic math functions, add, multiply or split.

So my question is, under what circumstances does it make sense to rewrite in C? Where will you see a significant speedup, and where can you see a reasonable speed without overwriting the vast amount of the program?

+3
source share
5 answers

If you are doing vector processing, give numpy a try first. Most likely you will get speed near C if you use the numpy vector manipulation functions wisely.

Other than that, your question is very heuristic. If your code is too slow:

  • Profile - maybe you can improve it in Python
  • Use the right optimized C-based libraries (numpy in your case)
  • Try psyco
  • cython
  • , C
+14

,

+9

-, C , :

  • , 1% , 1%

:

  • , , . 3.

, , . , .

python Linux pyprof2calltree, kcachegrind, .

+1

Profiler, Linux, pycallgraph - , , . , .

0

Common wisdom is "profile", "measurement", etc. Well, maybe. Just go into the debugger and grab 10 stackshots . If more than one of them ends in your shell code, then it costs about 10%, so you should consider re-executing this in C to save that time. Most likely, you will find other things that are more expensive.

0
source

Source: https://habr.com/ru/post/1729120/


All Articles