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

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.

Related

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

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.

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.

How do you use Python with C and Java simultaneously? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Edit<<<<<<<
The question is:
-How do you launch C code from python? (say, in a function)
-How do you load Java code into python? (perhaps in a class?)
-Can you simply work with these two in a python program or are there special considerations?
-Will it be worth it, or will integrating cause too much lag?
Being familiar with all three languages (C, Java and Python) and knowing that Python supports C libraries, (and apparently can integrate with Java also) I was wondering if Python could integrate a program using both languages?
What I would like is fast flexible C functions while taking advantage of Java's extensive front-end libraries and coordinating the two in Python's clean, readable syntax.
Is this possible?
EDIT---->
To be more specific, I would like to write and execute python code that integrates my own fast C functions. Then, call Java libraries like swing to create user interface and handle networking. Probably taking advantage of XML as well to aid in file manipulation.
To be more specific, I would like to write and execute python code that integrates my own fast C functions. Then, call Java libraries like swing to create user interface and handle networking. Probably taking advantage of XML as well to aid in file manipulation.
Integrating C code into Python is quite easy using modules such as ctypes or cffi.
Integrating Java code into Python is not simple, and is likely well beyond your ability. There are plenty of very capable user interface, networking, and XML processing libraries available for Python; you don't need Java to do any of that.
For C, you can use ctype module or SWIG.
For Java, Jython is a good choice.

Would a language written in Python using PLY be slow? [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
I'm interested in creating my own programming language and I would like to use python. My question is, would a language written in Python using the PLY library be considerably slower than CPython or would they be about the same in terms of program execution speed?
Also in terms of performance how much better would it be if I implemented it in C?
Thanks,
Francis
If you are implementing a compiler in PLY, the compilation may take longer - but that's irrelevant the execution speed of your program.
For example, you could use PLY to write a C compiler. The compiler may or may not be faster than your other C compiler, but the resulting executable should run at a similar speed (unless you miss a lot of optimisations etc.)

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.

Categories