新建文本文档,填入以下python代码(gpt4.0写的)
重命名:图片加载测试.py
双击运行,或者cmd输入:python 图片加载测试.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import requests import time
def test_image_loading_speed(image_urls): for url in image_urls: try: start_time = time.time()
with requests.get(url, stream=True) as response: response.raise_for_status()
chunk_size = 1024
for _ in response.iter_content(chunk_size=chunk_size): pass
end_time = time.time()
total_time = end_time - start_time print(f"图片URL: {url} 加载时间: {total_time:.2f} 秒") except requests.exceptions.RequestException as e: print(f"加载图片 {url} 时发生错误: {e}")
if __name__ == "__main__": input_urls = input("请输入图片URL,使用空格隔开: ") image_urls = input_urls.split()
test_image_loading_speed(image_urls)
input("测试完成,按任意键退出...")
|