python批量测试网站加载时间_如何使用Python的timeit计时代码段以测试性能?

1586010002-jmsa.png

I've a python script which works just as it should, but I need to write the execution time. I've googled that I should use timeit but I can't seem to get it to work.

My Python script looks like this:

import sys

import getopt

import timeit

import random

import os

import re

import ibm_db

import time

from string import maketrans

myfile = open("results_update.txt", "a")

for r in range(100):

rannumber = random.randint(0, 100)

update = "update TABLE set val = %i where MyCount >= '2010' and MyCount < '2012' and number = '250'" % rannumber

#print rannumber

conn = ibm_db.pconnect("dsn=myDB","usrname","secretPWD")

for r in range(5):

print "Run %s\n" % r

ibm_db.execute(query_stmt)

query_stmt = ibm_db.prepare(conn, update)

myfile.close()

ibm_db.close(conn)

What I need is the time it takes to execute the query and write it to the file results_update.txt. The purpose is to test an update statement for my database with different indexes and tuning mechanisms.

解决方案

You can use time.time() or time.clock() before and after the block you want to time.

import time

t0 = time.time()

code_block

t1 = time.time()

total = t1-t0

This method is not as exact as timeit (it does not average several runs) but it is straightforward.

time.time() (in Windows and Linux) and time.clock() (in Linux) are not precise enough for fast functions (you get total = 0). In this case or if you want to average the time elapsed by several runs, you have to manually call the function multiple times (As I think you already do in you example code and timeit does automatically when you set its number argument)

import time

def myfast():

code

n = 10000

t0 = time.time()

for i in range(n): myfast()

t1 = time.time()

total_n = t1-t0

In Windows, as Corey stated in the comment, time.clock() has much higher precision (microsecond instead of second) and is preferred over time.time().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值