Python C/C++ Wrappers versus Pure C/C++ Performance [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been doing some projects in computer graphics that have revolved around using open source libraries written and C/C++ that were then turned into wrappers for python.
I want to know if the performance gains for turning it into pure C/C++ would be worth the significant time it would take to rewrite the code base.
I also know C/C++ is almost always faster than python, but considering the libraries are already a C/C++ wrapper I'm not sure how much of a performance increase I should expect. I'm not looking for an exact answer since it very much depends on the circumstances, but if anyone has a general rule of thumb that'd be great!

Without seeing the code, in general it depends on
the granularity of the API, i.e. how much work is done by Python in relation to native code (or how often is control returning to Python),
whether Python code is on the critical path (some computer graphics libraries run the hot path in a separate, entirely native thread),
whether any compromise with regard to data structure in order to interface with Python was put in place.
Generally speaking, with a well-designed Python native library, there is little, if anything, to gain in terms of performance.
So I would start by profiling the Python code, to see if there is anything to gain.
Note also that C++ code is not fast by definition; it is only fast when it was engineered to be fast.

Related

Why are so many Python libraries written in C/C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In most languages, it's safe to assume libraries are written in the same language, ie. a Java library is usually written in Java.
In Python, that doesn't seem to be the case. Many of the high performance libraries such as numpy, pandas and more are written in C/C++, and provide Python bindings for convenience. It seems we could call these C/C++ libraries instead of Python libraries.
Why is this?
Your question had the answer buried in it: "Many of the high performance libraries...are written in C/C++."
There are two reasons for calling a low-level language from a language like Python, and performance is one of them. Numpy, for example, achieves a lot of its performance by carefully managing (and reusing) memory, and calling it from Python avoids the garbage collection overheads of writing the same functions in Python.
The other reason to call libraries written in another language is to do things not possible in the source language, such as taking advantage of non-blocking I/O system calls, or using special features of a platform such as vector processing instructions, or GPGPUs.

Is python a good choice to build a desktop application? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm wondering if python is a good choice to develop a desktop application for a small business.
Is it possible to build something using PyQT or even Swing + Jython ? How I can make a executable file at the end?
I've found Python to be an excellent choice for developing a broad scala of applications including desktop applications. I've developed in C++ for many years, and for parts that are really time critical I sometimes use it still, but for most of my code Python helps me get results much faster. There are several ways to get an executable. Py2exe is one of them, Cython another option allowing easy combination with C++ if you need raw speed, interfacing with 3rd party libaries or low level control of devices. It also makes reverse engineering a bit harder, if that's a concern in your project.
By the way, when I started out with Python I was very concerned about performance and (being a quality manager at that time) almost blocked its introducion in our company for its lack of strickt typing. I was wrong about both. Since especially the rich set of built in datastructures as well as many of its standard modules are written in C++ and have been carefully optimized, it isn't so easy to beat on speed and memory efficiency as one might think. And the dynamic typing (only recently introduced in a language like C#) proved a very powerful means to write concise, readable, reliable and compact code.
I don't have shares (since Python is open source anyhow) but from all the languages I've used in the past (Algol, Pascal, Modula II, Assembler, Basic, Fortran, Cobol, C, C++, C#, Java, JavaScript) it's the one I prefer rightnow to earn a living with.
It depends on what you are looking to do. I have done it and it worked but there are other questions that can factor in like who will maintain or update the code, and do they know Python. You can look at cx_freeze for making a windows executable out of a python program as one of many options.

Python: Is exec always bad practice and if so why not deprecated [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Can someone think of an example with good practice that uses exec?
If there is always a more efficient and secure way to replace exec, why python doesn't deprecate exec?
As explained in other questions, eval/exec are considered bad practice because they're generally abused to do a task where they aren't needed, leading to potential security issues and generally bad programming.
There are, however, valid uses for these mechanisms, and they expose important functionality that is not available elsewhere - executing arbitrary code at runtime.
Imagine, for example, that you want write a application that updates itself. You could fetch a script from a remote URL that runs with exec and updates your application to the latest version. While doing something like that, by itself, would pose a great security hazard, it's not hard to make the process secure through the use of digital signatures.
You can find another common use in the code module source: executing code input from the user at runtime for debugging purposes.
No, eval and related tools are not always bad by any measure.
There are a number of things that only work well when they are expressed as regular functions with regular, positional or keyword arguments (not magic *args or **keywords args). There's no way to dynamically create a function with a desired set of arguments, except with eval.
For a good example of how this can be used, examine the implementation of collections.namedtuple. Although it would be possible to create a class factory like that without eval, but all of the functions it defines, __new__ and _replace in particular, would have useless help text, and would be a less convenient tool without it. Worse, the non-eval implementation would almost certainly be SLOWER.
Another, more sweeping example of this exact use of eval is the fine decorator library, which generalizes this practice in a collection of tools that allow you to dynamically create functions with particular function signatures; it uses eval internally.

why do people say that RPython is an unpleasant language to program in [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
It is often stated that RPython is an unpleasant language to program in, for example, here, here, here or here.
However, for example here in the original paper about RPython, it says quite the opposite:
The result is a language that is more expressive than C# and Java, but
which does not compromise runtime efficiency. RPython was initially
designed for the specific purpose of implementing PyPy [25] (a Python
interpreter written in Python), but it has grown into a full-fledged
language in its own right.
Currently, RPython can be used in many contexts: to develop
stand-alone programs, such as the Standard Interpreter itself; to
write highly efficient extension modules for CPython, which could only
be written in C in the past; to develop dynamic web applications
without the need to write JavaScript code; to produce efficient
libraries of classes and functions to be used by other .NET and Java
programs. In particular, RPython can be the ideal companion for all
those CPython, IronPython and Jython developers that so far have been
forced to write the parts of their programs that need high performance
in C, C# or Java.
A related question for using RPython as a general purpose language is also here. I was also wondering about using RPython as a replacement for Cython. A related question is here. There is also the RPythonic project.
Why is it that people recommend against using RPython?
from here:https://mail.python.org/pipermail/pypy-dev/2013-June/011503.html
"
When people look at RPython, an obvious feature is that it is
syntactically identical to Python. "RPython must be an easy language,
given that it has got the syntax of Python, which is easy". This is a
common misconception. In fact, pleasing the automatic type inference
process can be difficult. It requires the programmer keeping in his
head the global types of his whole program, and carefully writing code
according to these implicit types. The process is much harder for
newcomers, which don't have any written-down example to learn how to
manipulate the types --- precisely because they are implicit.
"
I have made one file conversion program using RPython toolchain. Simple buffered file input and output. I can't imagine with my skills easier language to make such a fast, reliable, low bugs program.
What works and what doesn't is not well documented but once you find things out it is really nice set of tools to make small, fast and reliable programs.

Reference manual for python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Is there a recommended reference manual for python that's better than the official docs? I'm an experienced programmer (PHP, C#, javascript, and some C most recently), and I find the python manual pretty lacking compared the PHP manual and MSDN. In particular, the official docs never seem to tell me what errors can happen if I pass in something invalid, and there's apparently not a way to navigate within a module.
Take the os module for example. There's no list of constants or methods I can call on that page, so I have to Ctrl+f for "stat(" until I find it. Then, once I do find it, it doesn't tell me what to expect if I call stat with a directory that doesn't exist, so I just have to try it in a terminal and see what happens.
This seems wildly inefficient… how do python programmers deal with this?
In practice, you either make a quick test program to check the behavior, or read the source code. Much of the Python standard library code is fairly clearly written and in fact rather self-documenting, so it's standard practice to refer to it when you need to know the nitty-gritty details of how something works.
One exception: with low-level system functions such as many of those in the os module, the functions map directly on to their C namesakes for the underlying platform. So if you need to know about the behavior of Python's stat, you look up the reference documentation for your platform's native C stat call. In these cases the Python library docs often only explain the basic purpose of the function and how it differs from its C equivalent, if at all.
I don't think I ever had the same feeling towards the python docs as you do but I did some times need to go out of my way some times for a better understand of how a part of python works. Though that is how most language are. Python is a quick and easy language to learn which requires less time on docs and more time programming. Also python's user base isn't as large as PHP. PHP has people constantly giving examples and details on certain functions daily.
Another great thing about python is its interactive shell to test things out. Just my idea of programming you learn more by doing then seeing. So if you're ever interested in testing something you don't need to drag through compiling, just write a quick script in the interpreter. the interpreter also has reference tools. Example dir(<identifier>) and help(<identifier>) for module, clasa or function needs.
Any way enough defending my favorite language, pyDoc is a little useful tool to help you get what you need from python.
pydoc is a tool that comes with Python that can be used for viewing
and generating Python documentation.

Categories