计算程序执行时间的脚本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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值