Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Python worth the cost?

I'm looking at implementing a fuzzy logic controller based on either PyFuzzy (Python) or FFLL (C++) libraries.

I'd prefer to work with python but am unsure if the performance will be acceptable in the embedded environment it will work in (either ARM or embedded x86 proc both ~64Mbs of RAM).

The main concern is that response times are as fast as possible (an update rate of 5hz+ would be ideal >2Hz is required). The system would be reading from multiple (probably 5) sensors from an RS232 port and provide 2/3 outputs based on the results of the fuzzy evaluation.

Should I be concerned that Python will be too slow for this task?

like image 519
NBenatar Avatar asked Sep 30 '09 13:09

NBenatar


3 Answers

In general, you shouldn't obsess over performance until you've actually seen it become a problem. Since we don't know the details of your app, we can't say how it'd perform if implemented in Python. And since you haven't implemented it yet, neither can you.

Implement the version you're most comfortable with, and can implement fastest, first. Then benchmark it. And if it is too slow, you have three options which should be done in order:

  • First, optimize your Python code
  • If that's not enough, write the most performance-critical functions in C/C++, and call that from your Python code
  • And finally, if you really need top performance, you might have to rewrite the whole thing in C++. But then at least you'll have a working prototype in Python, and you'll have a much clearer idea of how it should be implemented. You'll know what pitfalls to avoid, and you'll have an already correct implementation to test against and compare results to.
like image 112
jalf Avatar answered Oct 03 '22 12:10

jalf


Python is very slow at handling large amounts of non-string data. For some operations, you may see that it is 1000 times slower than C/C++, so yes, you should investigate into this and do necessary benchmarks before you make time-critical algorithms in Python.

However, you can extend python with modules in C/C++ code, so that time-critical things are fast, while still being able to use python for the main code.

like image 38
Lars D Avatar answered Oct 03 '22 11:10

Lars D


Make it work, then make it work fast.

like image 35
KevDog Avatar answered Oct 03 '22 13:10

KevDog