我正在努力找出如何分析一个简单的多进程python脚本
import multiprocessing
import cProfile
import time
def worker(num):
time.sleep(3)
print 'Worker:', num
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
cProfile.run('p.star
我在python中工作,我有一个python脚本,从文本文件中读取数据并保存到数据库中。
我得到了一些要求,想找出以下3点
1. Total execution time taken by the script for running
2. CPU utilization (For process performing by the python script)
3. Memory usage (For process performing by the python script)
对于第一点,即,我找到了脚本处理(读取文本文件并保存到数据库)所用的总执行时间,如下所示
python -m
我正在研究python,并发现了一些寻找代码的统计数据和执行时间的概念。
假设我有以下代码
from time import gmtime, strftime
import timeit
def calculation():
a = 2
b = 3
res = a + b
return res
if 'name' == 'main' :
exec_time = timeit.timeit(calculation)
print exec_time
结果:
0.2561519145965576
所以,从
我正在实验cProfile来分析python applications.so,我编写了一个简单的脚本,下面是我的结果。
def foo():
for i in range(100000):
print i
def bar():
for i in range(100):
print "bar"*i
foo()
bar()
当我以python -m profile script.py的形式运行上面的脚本时,我得到了以下输出:
7 function calls in 0.136 seconds
Ordered by: standa
在Python2.7和Python3.6中,我发现这是可行的:
from cProfile import Profile; p = Profile(); p.enable()
...whereas这会引发一个异常:
from profile import Profile; p = Profile(); p.enable()
# --> AttributeError: 'Profile' object has no attribute 'enable'
这让我感到惊讶,因为我认为(以及和状态的官方文档)这两个模块应该提供相同的程序员接口:
profil