计算程序执行时间的脚本python3.6(Win7x64)#windows  python
#-*- coding: utf-8 -*-


from __future__ import print_function
import datetime
import time
import os
import win32com.client
import xlwt
import ctypes, sys


#output all process
#import psutil


#pids = psutil.pids()
#for pid in pids:
#    p = psutil.Process(pid)
#    print("pid:%d,    pname:    %s" %(pid,p.name()))


starting_time = []
stoping_time = []


def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False

def check_exsit(process_name):
WMI = win32com.client.GetObject('winmgmts:')
processCodeCov = WMI.ExecQuery('select * from Win32_Process where Name="%s"' % process_name)
if len(processCodeCov) > 0:
#        print('%s is exists' % process_name)
return 0
else:
#        print('%s is not exists' % process_name)
return 1


def start_server():
#get start time
#pre = time.strftime("%H:%M:%S")
#pre = time.time()
start = time.clock()


#start server
os.system("server start")


var=1
while var == 1:
if check_exsit("server.exe") == 0:
break



#get endtime
#now = time.time()
#print("before start %f" % now)


end = time.clock()
print('staring time: %s Seconds'%(end-start))
return end-start




def stop_server():
#get start time
#pre = time.strftime("%H:%M:%S")
#pre = time.time()
start = time.clock()


#start server
os.system("server stop")


var=1
while var == 1:
if check_exsit("server.exe") == 1:
break



#get endtime
#now = time.time()
#print("before start %f" % now)


end = time.clock()
print('stoping time: %s Seconds'%(end-start))
return end-start

if is_admin():
count=50
while(count != 0):
starting_time.append(start_server())
stoping_time.append(stop_server())
count=count - 1
print(count)

#write ti excel
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')

wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet('Sheet1')

#ws.write(0, 0, 1234.56, style0)
#ws.write(1, 0, datetime.time(), style1)
#ws.write(2, 0, 1)
#ws.write(2, 1, 1)
#ws.write(2, 2, xlwt.Formula("A3+B3"))


ws.write(0, 0, "starting")
ws.write(0, 1, "stoping")


i = 1
for var in starting_time:
ws.write(i, 0, var)
i = i + 1

j = 1
for var in stoping_time:
ws.write(j, 1, var)
j = j + 1


wb.save('example1.xls')
#i=0
#while i < 3:
#    print(i)
#    print(cmd_time[i])
#    ws.write(i, 0, cmd_time[i])
#    i = i + 1
else:
if sys.version_info[0] == 3:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
else:#in python2.x
ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)
#cxfreeze start50iSM1.py --target-dir dist --base-name=WIN32GUI