python只能以程序方式执行死刑_有没有办法加快这个python程序的速度?(短)

我不熟悉编程,所以如果下面程序中的逻辑没有意义,这可能就是原因。幸运的是,下面的代码运行并完成了我需要的所有事情,但感觉执行起来需要很长时间(每10000条记录需要6分钟)。在

该程序的目的是为数据库中的记录分配新的ID,并允许用户指定这些ID的增量值和起始点。在

说实话,我不完全确定死刑执行时间是否不合理,因为我没有很多经验可以作为依据,但如果有办法加快执行速度,我会洗耳恭听。在# generates study IDs for MS Access dataset

import pyodbc

import random

import time

startTime = time.time()

dbFile = 'C:\Backend.accdb'

conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'

+ 'DBQ=' + dbFile + '; Provider=MSDASQL;')

cursor = conn.cursor()

# shuffle the existing IDs so the assignment of the new IDs is random

a = []

sql = "SELECT ID FROM Clients"

for row in cursor.execute(sql):

a.append(row.ID)

print "\nIDs appended to list...\n"

random.shuffle(a)

print "\nlist shuffled\n"

# assign new IDs according to the conditions below

startPt = 900001

increment = 7

idList = {}

for i in a:

idList[i] = startPt

startPt += increment

# append new IDs to another table in the database

for j, k in idList.iteritems():

sql = "INSERT INTO newID values ('%s', '%s')" %(j,k)

cursor.execute(sql)

conn.commit()

# close connection

cursor.close()

conn.close()

# calculate, in seconds, the time the program took to execute

executionTime = str(time.time() - startTime)

print "completed. the program took %s seconds to execute." %executionTime

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值