项目欧拉和其他编码竞赛通常有最长的运行时间或人们吹嘘他们的特定解决方案运行的速度.使用python,有时候这些方法有点像kludgey - 即添加时间码__main__.
分析python程序运行多长时间的好方法是什么?
我得到一个网址:
r = requests.get("http://myserver.com")
Run Code Online (Sandbox Code Playgroud)
正如我在"myserver.com"的"access.log"中看到的那样,使用了客户端的系统代理.但我想完全禁用使用代理requests.
我是一名新的 Python 程序员,负责做一个简单的应用程序来进行 API 调用。它工作正常,但我对速度不满意。我已经在低速互联网连接和更快的互联网连接上尝试过它。平均而言,进行 API 调用所需的时间为 1.3 到 2.4 秒。我已经在 5mbps 到 80mbps 的互联网连接上进行了测试,两者都显示了相似的结果(加上负 0.5 秒的差异)。
在纸面上花费的时间看起来还不错,但是,当我检查进行调用的服务器时,处理时间仅为 0.2 - 0.5 秒左右,这意味着在请求过程中损失了 1 - 2 秒的时间过程。
这是我正在使用的代码:
import requests
import json
import time
import uuid
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
#get api call start time
startTime = time.time()
#make api call
r=requests.post(APIUrl, data=payload, headers=headers, verify=False)
#get api call finish time
endTime = time.time()
#calculate time taken
totalTimeTaken = str(float(round((endTime - startTime ),3)))
json_obj = r.json(strict=False)
print "Response: "+str(json_obj['response'])
print "Elapsed: "+str(r.elapsed)
print …Run Code Online (Sandbox Code Playgroud) 我正在开发一个下载管理器.使用python中的请求模块来检查有效链接(并希望破坏链接).我检查以下链接的代码:
url='http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe'
r = requests.get(url,allow_redirects=False) #this line takes 40 seconds
if r.status_code==200:
print "link valid"
else:
print "link invalid"
Run Code Online (Sandbox Code Playgroud)
现在,问题是执行此检查需要大约40秒,这是巨大的.我的问题是如何使用urllib2或其他东西加快速度呢?
注意:如果我替换url为实际的URL" http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe ",这需要一秒钟,因此它似乎是请求的问题.