Python syntax is simple, and by indenting the way to express hierarchy, the code is very concise and easy to understand, for beginners, it is easier to get started.
The pattern matching of Perl is very powerful, and there are many kinds of matching symbols, which are difficult to read and maintain.
In terms of text processing, Python implements the find and replace of pattern matching by loading the RE module. And Perl has a pattern-matching feature built into it.
Note: The difference between built-in commands and external commands.
Make comparisons directly from the code.
Python version:
#!/usr/bin/python
import re
import fileinput
exists_re = re.compile(r'^(.*?) INFO.*Such a record already exists', re.I)
location_re = re.compile(r'^AwbLocation (.*?) insert into', re.I)
for line in fileinput.input():
fn = fileinput.filename()
currline = line.rstrip()
mprev = exists_re.search(currline)
if(mprev):
xlogtime = mprev.group(1)
mcurr = location_re.search(currline)
if(mcurr):
print fn, xlogtime, mcurr.group(1)
Perl version:
#!/usr/bin/perl
while (<>) {
chomp;
if (m/^(.*?) INFO.*Such a record already exists/i) {
$xlogtime = $1;
}
if (m/^AwbLocation (.*?) insert into/i) {
print "$ARGV $xlogtime $1\n";
}
}
Time process_file.py *log > Summarypy.log
Real 0m8.185s
User 0m8.018s
SYS 0m0.092s
Time process_file.pl *log > Summaypl.log
Real 0m1.481s
User 0m1.294s
SYS 0m0.124s
in terms of text processing, Perl is about 8 times times faster than Python .
So in dealing with large files such as large logs, Perl is better, because faster.
If the speed requirement is not very strict, Python is better, because Python is simple, easy to maintain and read.
Why is perl much faster than Python when it comes to text processing?
This is because Perl's pattern matching is its built-in functionality, and Python needs to load the RE module, using built-in commands much faster than external commands.
The difference between built-in commands and external commands the Linux command has built-in commands and external commands that are basically the same, but the calls are slightly different.
built-in commandsis actually part of the shell program, which contains simple Linux system commands that are identified and run inside the shell program in the shell program, usually
Linux system Load runtime shell is loaded and resides in system memory。 Internal commands are located in the bash source code, which executes faster than external commands because
Parse Internal command shell does not need to create child process, such as Exit,cd,pwd,echo,history.
external Commandis a utility application in a Linux system, because the utility is usually powerful and contains a large number of programs,
when the system is loaded, it is not loaded into memory with the system, but is transferred into memory when needed。 Entities that are normally external commands are not included in the shell, but their command execution procedures are controlled by the shell. The shell program manages the path lookup of the external command execution, loads the store, and controls the execution of the command. External commands are installed outside of bash, usually on/bin,/usr/bin,/sbin,/usr/sbin,.... such as
Use the type command to distinguish between internal and external commands.
Comparison of Perl and Python (mainly performance comparisons)