In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python and C++ use which is good", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python and C++ which is better to use"!
Python is an interpreted language, C++ is not.
C++ works by first saving the written code to a file with the extension.cpp.
Then compile the.cpp file. Compilers convert C++ code into native code. and execute that machine code. C++ is very close to hardware.
So C++ is very fast and very suitable for real-time applications. The key is that C++ applications are "native" applications, which means that compilers are operating system and processor dependent components.
Compilers are machine dependent, so C++ is machine dependent.
However, this means that the code can only run on operating systems (and processors) compatible with the compiler that compiled the code. Specifically:
If you compile code on a Windows machine with the Windows compiler, Windows machine code is generated.
If you compile cpp code using a Linux compiler, the compiled code can only run on Linux machines.
Sometimes, some C++ modules/packages are not compatible with all operating systems. Python, on the other hand, is an interpreted language. Python works by first creating a virtual machine where the Python package is installed.
Then save the Python code to the.py file.
The Python code is then compiled into bytecode for the Python virtual machine via CPython.
So, this Python VM is machine-dependent like C++, but Python code is not.
When you want to execute bytecode, the code is interpreted at runtime. Python is not machine dependent.
Python bytecode relies on Python virtual machines, while Python is machine-independent.
One thing to note is that we can write Python code in one operating system and copy it to another operating system to run. C++ is not. We have to compile C++ code on every OS. That's why C++ runs so fast compared to Python.
Since Python is an interpreted language, it runs slower than C++.
programming convention
First, let's look at ease of use.
Python is a high-level language and C++ is a low-level language. Python is readable, simple, straightforward, and easy to learn.
This is a big advantage because it means more and more developers are adopting Python. And this feature can quickly bring Python applications to market.
Due to its ease of use and large number of libraries, Python has become the programming language of choice for data science and machine learning projects, and it is also gaining popularity in Web development.
Python is great for testing new concepts and ideas.
However, this is sometimes the reason programmers treat Python as a prototype language. But then again, there are good reasons for this view. When implementing Python, we should always follow best software principles and practices.
Python is a great language for people new to programming.
C++, on the other hand, originated from the C language. It is a powerful language and therefore complex because it can perform many low-level tasks.
Developers need to adhere to a large number of conventions and rules in C++.
C++ is widely used in game development, back-end server applications, and distributed transaction applications, all of which require fast execution of tasks. In addition, C++ is extremely portable.
Now let's talk about grammar.
1. Python programming relies heavily on whitespace and tabs (indentation). Everything is an object. All class properties and methods are public. In C++ we can hide attributes/methods of classes with access modifiers, but Python can't.
I had a hard time getting used to Python indentation and formatting at first.
2. I think most developers know that there are no semicolons or braces in Python. You can write neat list derivations. Boolean expressions in Python also have no parentheses. C++, on the other hand, relies heavily on parentheses and semicolons. Getting used to C++ syntax takes time.
3. C++ is a strongly typed language, and the type of each variable needs to be declared in advance. Python is dynamically typed, so we don't need to specify the type of the object. This dynamic nature often leads us to unexpected results. Python programmers need to ensure that objects are used in the right way or in the right context, so there are both positives and negatives.
While executing Python code, we encounter runtime errors. If you encounter type errors during production runtime, it can be embarrassing.
It is worth mentioning that many developers spend a lot of time and effort to implement Python solutions that can detect problems before they run.
Memory management and concurrency
Before describing how memory is managed, we should first note that both programming languages support object-oriented programming and inheritance.
When programmers create objects in programs, these objects consume memory. When the object is not in use, the program removes this garbage through garbage collection.
The way garbage collection works is uncertain.
As Python programmers, we don't have to worry about how and when to clear memory. Python automatically handles memory for us through intelligent garbage collection. There is no such garbage collection in C++, you have to manage the memory yourself.
Memory management in C++ needs to be done manually.
For good reason. C++ was designed to be a high-performance programming language. Garbage collection, which is responsible for managing memory and removing unused objects from memory, affects application performance.
Most importantly, garbage collection is inherently indeterminate. You cannot guarantee that objects will remain in memory after deletion.
Python is a very developer-friendly programming language because we don't have to worry about forgetting to free up memory.
concurrent
For I/O or CPU constrained applications, programmers often like to create multiple threads in their code and speed up computations by running them simultaneously.
No matter how many cores we have in our computer, Python only allows us to execute one thread at a time. This is a limitation imposed by Python's global interpreter. This can cause problems for applications that require multiple CPUs.
However, we can create multiple processes in Python.
C++, on the other hand, allows us to implement multithreaded applications.
C++ has pointers, but Python doesn't.
How can we talk about C++ without mentioning pointers?
There are no pointers in Python, at least not out of the box. However, C++ has pointers.
First, let's explain what a pointer is.
Suppose there is one variable. This variable is an integer set to 5. Then, a variable and its value have the same meaning, for example x = 5, x is the name of the variable, and the value is 5.
In C++, variables are passed to functions by value.
So the question is, where are these values stored?
Each value of a variable is stored in a memory address of the computer. You can use the C++ operator & to access memory addresses of variables. A pointer is a variable that points to the memory address of the value.
Pointers can improve the efficiency of programs.
A pointer can be declared by using the operator * before the identifier. As follows:
int* y = &x
Above, we create a pointer variable y that holds the memory address of variable x.
Suppose you have a function that accepts variables as input (arguments). Changing this value means creating a new variable inside the function. Keep in mind that this takes up a lot of memory in C++, where variables are passed to functions by value.
Specifically, first we create a function that adds 100 to the incoming variable.
void add_hundred(int x) { x += 100; }
This code takes a variable x and adds 100 to x.
Inside the function, any changes to this value are made to a new variable, not the original variable.
The main function that calls the above code is as follows:
int main(void) { int x = 2337; add_hundred(x); return 0; }
Even after the function is executed, the value of x outside the function is still 2337.
So why do we need pointers?
To understand this, you must understand the difference between a value and a reference type. Value types are like physically copying a variable and assigning it a new variable each time. Value types take up more space, and each variable has its own memory address.
We can modify the above function to accept pointers as arguments. Such modifications can reduce the amount of memory used by the program. And it doesn't create unnecessary duplicate variables.
The following function modifies the value of a variable directly:
void add_hundred(int *x) { *x += 100; }int main(void) { int x = 2337; int *y = &x add_hundred(y); return 0; }
Next, we create a pointer y and pass it to a function that adds 100 to the input parameter x, even if the value of x is outside the function. That is, after the execution of the function, x has a value of 2437.
What I think about the needle
One of the reasons for using pointers is that you can modify variables or objects in the function you are calling. However, I recommend avoiding pointers as much as possible.
In C++, references are better than pointers because you can easily modify the function being called without changing the semantics represented by the call.
Pointers are a complex subject, and programmers often make memory management errors by using pointers. Python is a programming language for beginners, so there are no such complex types in Python.
the final recommendations
What do I suggest? Python or C++?
It depends on the solution you need to implement. Do you need a cross-platform, high-performance, machine-learning solution?
If possible, you can use both at the same time.
I strongly recommend that you learn and use both, as this broadens your understanding of programming languages.
If you are familiar with C++, you can write programs in Python without any effort.
If you are familiar with Python, you should have a good understanding of most programming concepts, C++ can help you further understand memory management, concurrency, pointers, etc., so both should be learned.
In addition, you can integrate Python and C++ together for advanced real-time analytics solutions.
At this point, I believe that everyone has a deeper understanding of "Python and C++ which is good to use", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.