os 方法性能监测方法

 1 # -*- coding: utf-8 -*-
 2 __author__ = 'admin'
 3 
 4 
 5 #主要是目录相关的操作
 6 
 7 import os
 8 
 9 import shutil
10 
11 
12 #os.mkdir('download')
13 #os.rmdir('download')
14 
15 #os.makedirs('a/b/c',)
16 
17 #os.removedirs('a/b/c')
18 os.chdir('.')
19 
20 #print os.getcwd()
21 #print os.listdir('.')
22 #os.chdir('./static')
23 #print os.listdir('.')
24 
25 base=os.getcwd()
26 #alllist=[base + '/' + x for x in os.listdir('.')]
27 
28 #alllist2=map(lambda x:os.path.join(base, x),os.listdir('.'))
29 from time import time
30 def cost_time(func):
31     def result(*args,**dic):
32         beign=time()
33         func(*args,**dic)
34         print "cost time : ",time()-beign
35     return result
36 
37 
38 def listfile(path):
39     result=[]
40     g=os.walk(path)
41     for p,folder,filelist in g:
42         for fname in filelist:
43              result.append(os.path.join(p,fname))
44     return result
45 
46 
47 def listfile2(path):
48     result =[]
49     allfile=os.listdir(path)
50     for filename in allfile:
51         fullName=os.path.join(path,filename)
52 
53         if os.path.isdir(fullName):
54             #result=result+listfile2(fullName)
55             result.extend(listfile2(fullName))
56         else:
57             result.append(fullName)
58 
59     return result
60 
61 @cost_time
62 def method1():
63     print(listfile('/Users/znsearch/Documents'))
64 
65 
66 @cost_time
67 def method2():
68     print(listfile2('/Users/znsearch/Documents'))
69 
70 
71 #print(listfile2('./static'))
72 #print('\n')
73 
74 
75 method1()
76 print('\n')
77 method2();

 

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/znsearch/Documents/dev_Flask/sample1/tryos.py
cost time :  0.0926008224487

 

posted @ 2014-02-25 22:51  zhangxiaodel  阅读(215)  评论(0编辑  收藏  举报