python压缩文件的效率高吗?这里使用shell脚本和Python脚本同样压缩文件进行对比,通过时间和压缩比对比发现Python的压缩比可以,但是效率低耗时较多。
Shell使用系统tar
Python使用tarfile模块,Python不开多线程的情况两者做文件打包压缩对比。
Python代码:
#-*- coding: utf-8 -*- #!/usr/bin/evn python """ USAGE: Bak_rsync_Python.py version """ import sys import time import os import shutil import subprocess import tarfile rundir=os.getcwd() src_dir='/data/rsync_center' bak_dir='/data/backup/game/test' today=time.strftime("%Y_%m_%d", time.localtime()) def main(version): #set local var md5="/sbin/md5" bak_file="rsync_center_"+today+"_"+version+".tgz" #多个变量联合做变量 md5_file=rundir+"/md5/"+bak_file+".md5" cmd="%s %s > %s" % (md5,bak_file,md5_file) #print bak_file #print md5_file #tar src dir os.chdir(src_dir) tar=tarfile.open(bak_file,'w|gz') tar.add(src_dir) tar.close() #tgz file md5 ret=subprocess.call(cmd,shell=True) #subprocess.call返回值是退出状态 if ret !=0: print "md5 failed" sys.exit(1) #else: print ret #move tgz to dst dir shutil.move(bak_file,bak_dir) if __name__=='__main__': try: len(sys.argv)!=2 except: print "Please give version" print __doc__ else: print "start at",time.ctime() version=sys.argv[1] main(version) print "end at",time.ctime()
shell脚本:
#!/bin/sh - # Bak_rsync_Shell.sh version shell_dir=`pwd` src_dir=/data/rsync_center bak_dir="/data/backup/game/test" log_file=${shell_dir}/rsync_center_bak.log Version=$1 Today=`/bin/date +%Y_%m_%d` Fourteenday=`/bin/date -v -8d +%Y_%m_%d` bak_file="rsync_center_${Today}_${Version}.tgz" md5_file=${shell_dir}/md5/$bak_file.md5 if [ $# -ne 1 ] then echo "Please give a version!" break else startdate=`date "+%Y-%m-%d %H:%M:%S"` echo "rsync_center backup Start at " $startdate cd $src_dir tar -zcf $bak_file ./ /sbin/md5 $bak_file > $md5_file mv $bak_file $bak_dir/ enddate=`date "+%Y-%m-%d %H:%M:%S"` echo "rsync_center backup Complete at" $enddate fi
运行对比:
#du -sh /data/rsync_center/ 112M /data/rsync_center/ #sh Bak_rsync_Shell.sh testShell rsync_center backup Start at 2012-03-15 14:53:37 tar: ./rsync_center_2012_03_15_testShell.tgz: Can't add archive to itself rsync_center backup Complete at 2012-03-15 14:53:46
使用了9秒
#python Bak_rsync_Python.py testPython start at Thu Mar 15 14:54:54 2012 end at Thu Mar 15 14:55:20 2012
使用了27秒
压缩大小差不多
#ll -h /data/backup/game/test/ | awk '{print$5,$9}' 49M rsync_center_2012_03_15_testPython.tgz 49M rsync_center_2012_03_15_testShell.tgz
python使用代码53行(取出注释也有40行),shell使用代码26行
python的逻辑更复杂,shell更简单一点。
可见python在单线程的情况下,对于系统基本文件处理,比如复制移动,打包压缩不如shell简单高效。逻辑上也不利于SA快速理解。所以,Python在基本的系统管理上,可能不如Shell.
The above is the detailed content of Is python compressed files efficient?. For more information, please follow other related articles on the PHP Chinese website!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
