Actually, Python runs faster than we think. The reason we have a preconceived notion that Python is slow is probably due to our usual misuse and lack of knowledge of how to use it.
Next let's see how we can improve the performance of our program with some simple Trick
1, the use of built-in functions
Python
Many of the built-in functions in are implemented in C and are well optimized. Therefore, if you are familiar with these built-in functions, you can improve the performance of your Python code. Some of the commonly used built-in functions aresum()
、len()
、map()
、max()
etc.
Suppose we have a list of words and we want the first letter of each word to be capitalized. This is done by using themap()
Functions are good choices.
General version.
new_list = [] word_list = ["i", "am", "a", "python", "programmer"] for word in word_list: new_list.append(())
Improved version.
word_list = ["i", "am", "a", "python", "programmer"] new_list = list(map(, word_list))
Time Comparison.
import time new_list = [] word_list = ["i", "am", "a", "python", "programmer"] start = () for word in word_list: new_list.append(()) print(() - start, "seconds") start = () new_list = list(map(, word_list)) print(() - start, "seconds")
Results.
1.0013580322265625e-05 seconds
4.76837158203125e-06 seconds
You can see that the second method runs nearly twice as fast.
2、String join VS join()
existPython
in which the strings are immutable, so we can't modify them.
Every time we concatenate multiple strings, we create a new string, which can cause some performance problems.
General version.
new_list = [] word_list = ["I", "am", "a", "Python", "programmer"] for word in word_list: new_list += word
Improved version.
word_list = ["I", "am", "a", "Python", "programmer"] new_list = "".join(word_list)
Time Comparison.
import time new_list = [] word_list = ["I", "am", "a", "Python", "programmer"] start = () for word in word_list: new_list += word print(() - start, "seconds") start = () new_list = "".join(word_list) print(() - start, "seconds")
Results.
4.0531158447265625e-06 seconds
9.5367431640625e-07 seconds
utilizationJoin()
Functions can make code run up to 4 times faster.
3. The way to create lists and dictionaries
In general, using [] and {} to create lists and dictionaries is much more efficient than using thelist()
cap (a poem)dict{}
run more efficiently. This is due to the use oflist()
cap (a poem)dict{}
to create an object requires an additional function to be called.
General version.
list() dict()
Improved version.
() {}
Time Comparison.
For the sake of time comparisons, we'll use thetimeit
function to statistics, we run 1 million times, to see the time comparison between the two, the code is as follows.
import timeit slower_list = ("list()", number=10**6) slower_dict = ("dict()", number=10**6) faster_list = ("[]", number=10**6) faster_dict = ("{}", number=10**6) print(slower_list, "seconds") print(slower_dict, "seconds") print(faster_list, "seconds") print(faster_dict, "seconds")
Results.
0.08825178800000001 seconds
0.083323732 seconds
0.019935448999999994 seconds
0.027835573000000002 seconds
As you can see, we're running nearly four times faster.
4. Using f-Strings
We already know that concatenating strings may slow down a program.
Another better solution is to use thef-Strings
。
General version.
me = "Python" string = "Make " + me + " faster"
Improved version.
me = "Python" string = f"Make {me} faster"
Time Comparison.
import time me = "Python" start = () string = "Make " + me + " faster" print(() - start, "seconds") start = () string = f"Make {me} faster" print(() - start, "seconds")
Results.
2.1457672119140625e-06 seconds
9.5367431640625e-07 seconds
As you can see, we're running nearly two times faster.
5. Use of Comprehensions
PythonList Comprehensions
This provides us with a shorter syntax, or even a single line of code, to achieve a variety of powerful features. In many scenarios where loops are used, we try to use generative syntax.
General version.
new_list = [] existing_list = range(1000000) for i in existing_list: if i % 2 == 1: new_list.append(i)
Faster version.
existing_list = range(1000000) new_list = [i for i in existing_list if i % 2 == 1]
Time Comparison.
import time new_list = [] existing_list = range(1000000) start = () for i in existing_list: if i % 2 == 1: new_list.append(i) print(() - start, "seconds") start = () new_list = [i for i in existing_list if i % 2 == 1] print(() - start, "seconds")
Results.
0.16418218612670898 seconds
0.07834219932556152 seconds
As you can see, we're running nearly two times faster.
6. Appendix - Built-in Functions in Python
We can check out the official websitePython
built-in functions.
If we just focus on the short code snippets in the above examples, these techniques don't seem to make much of a difference. In fact, our projects can easily become more complex, which is where these techniques come in handy!
7. Summary
This paper focuses on the role of thePython
How to use some simple Trick to improve the efficiency of code running, and give the corresponding code examples.