Performance Comparison Between the Python programming language and Java

Source: Internet
Author: User

The Python programming language must be continuously used. Next we will take a detailed look at how we can better learn relevant knowledge. Recently I have been reading a wxPython-based GUI application code, which is about 45.5KLOC, and does not include the Library (such as Twisted) it uses ).

The code is written by Java developers who are unfamiliar with Python, so it has serious performance problems (such as 30 seconds of startup time ). When checking the code, I found that they wrote a lot of things that can make sense in Java, but it is very unacceptable for the Python programming language. It is not because "Python is slower than Java", but because there are more convenient methods in Python to accomplish the same goal, or even impossible in Java.

So what's really sad is that these guys are doing a lot of work, and the code they write is much slower than what they should have done in a Python programming language. Next, let's look at some examples:

◆ Static methods in Java cannot be translated into Python class methods. Oh, of course, he can have the same effect more or less, but the purpose of the class method is actually to do something that is generally not even possible in Java (such as inheriting a non-default function ). The typical translation of Java static methods is usually translated into a module-level function, rather than a class method or static method. (And static constants should be translated into module-level constants .)
This is not a performance issue, but if a Python programming language programmer wants to call Foo. someMethod, if he is forced to use Foo. foo. someMethod will drive him crazy. Note: calling a class method requires an additional storage space, which is not required to call a static method or function.

By the way, there are also Foo. bar. baz's attribute chains cannot be counted by themselves. in Java, the names with vertices are searched by the compiler, and the total number of vertices is not considered during running. in Python, the search process is executed at runtime, so each vertex must be included. (in Python, remember that "tiled structures are better than nested structures", although compared to performance, it may be more involved in "readability" and "Simplicity is better than complexity ".)

◆ Do I need to use the switch statement? The Python programming language is a hash table, not a bunch of if-then statements. Should I use a bunch of if-then statements that are not a switch statement in Java and contain strings? It will still be a hash table. CPython dictionary is used in hash tables that we know as one of the best performances. The code you write will not be better than this one, unless you are an illegitimate child of Guido, Tim Peters, and Raymond Hettinger, and you are still genetic enhancement.

◆ XML is not the answer. It is not a problem either. Now we use regular expressions to explain Jamie Zawinski. "Some people, when he encounters a problem, will think, 'I know, I want to use XML. 'Then they have two problems."

This is different from Java, Because XML is flexible and flexible than Java code. However, XML is an anchor and cumbersome compared to Python code. In Python, XML is used to work collaboratively, not your core functions, because you do not need to do that. In Java, XML may be your savior because it enables you to implement languages in specific fields and improve your application adaptability without coding. In Java, avoiding encoding is a great advantage, because encoding means re-compilation. But in Python, writing code is usually easier than writing XML. In addition, Python processes code much faster than XML. (More than that, you have to write XML Processing code, and Python has already written it for you .)

If you are a Java programmer, you cannot use instinct to consider whether you want to use XML as part of your Python core application. If you do not implement an existing XML standard or create an input or output format or an XML editor or processing tool because of information interaction, do not do so. Do not do this at all. Don't even think about it. Now, discard the XML schema and free your hands! If your application or platform is to be used by Python programming language developers, they will only thank you for not adding the burden of using XML in their work.

(The only exception here is that if your customer (your target audience) does need to use XML for some reason. It's like they refuse to learn Python, but if you use XML, they will pay you, Or you plan to give them a great GUI that can edit XML, in addition, the xml gui is written by another user, and you are entitled to use it for free. There are also some rare architectural reasons that need to use XML. Believe me, they will not be applied to your program. If you have any questions, explain your use case to a senior Python developer. Or, if you have a thick face and don't mind being ridiculed, try to explain to a Lisp program why your program uses XML !)

◆ Getter and setter are demons. I should say it's a devil, it's a devil! The Python programming language object is not a Java Bean. Do not write any getter or setter, but also build them into the "attribute. It doesn't make sense until you prove that you need something more complex than a simple access. Otherwise, do not write getter and setter. They are a waste of CPU time, and more importantly, they are a waste of valuable time for programmers. Not only do users who write and test code, but also those who want to read and understand them.

In Java, you must use getter and setter, because public fields do not allow you to change your mind and use getter and setter again. Therefore, in Java, you 'd better avoid these "Chores" in advance ". in Python, this is silly because you can start with a common feature and change your mind at any time without affecting any customers of this class. Therefore, do not write the getter and setter methods.

◆ Repeated code in Java is usually an inevitable disaster. You must write the same method repeatedly and only change a little bit (usually because of static type constraints ). In Python, this is unnecessary and not worthwhile (except for a few specific scenarios that require inline functions that require performance ). If you find that you write the same code over and over again and the code changes little, you need to learn the closure. They are actually not so terrible.

  • Summary of Python programming skills
  • Python programming language with simplified read flexibility
  • Measure the test taker's knowledge about the Python programming language in a short time.
  • Introduction to the history of the Python Programming Language
  • Describes the Python programming language.

This is what you have to do. You have written a function that contains the function. The internal function is the template of the function you want to write over and over again, but the variable is added to the function for different situations. External functions need the variable just raised as the parameter, and internal functions are returned as the result. Then, every time you want to write another function that is slightly different, you only need to call this external function and assign the returned value to the name that you want to "repeat" the function. Now, if you want to change the way you work, you only need to change the template.

In the applications/platforms I have seen, only a very trivial program uses this technology, removing hundreds of lines of code. In fact, because developers use special sample files to develop plug-ins for this platform, this saves a lot of code from third-party developers, it also makes things easier for programmers to learn.

This is just the tip of the iceberg in the way of thinking in Java-> Python programming language. Now I can make a correct change without having to delve into the details of the program. In essence, if you have used Java for a while and are unfamiliar with Python, do not trust your instinct too much. Your instinct has been adjusted by Java, not Python. The most important thing is not to write so much code.

To do so, I feel that Python is more necessary. Pretend that Python can be any magic wand you want, and you don't have to make any effort. Ask, "How does Python solve my problem ?" "Which of the following is the most similar to my problem in Python ?" If something you need already has a fixed form, you will be surprised. As a matter of fact, this phenomenon is so common that it may even appear in a very experienced Python programmer that the Python community has named it. We call it the "GUIDO time machine", because it seems that it is the only way to get what we need before we have mastered it.

Therefore, if you cannot feel at least 10 times more productive when using the Python programming language than using Java, you 'd better make some changes. Do you forget to use the time machine! (Chances are good that you 've been forgetting to use the time machine) (if you miss your Java IDE, you can think like this: because the Python program you write is much more complex than what he needs .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.