根据这个答案,呼叫len(s)的复杂度为O(1).那么为什么在一个下载的27kb文件上调用它比在一个1kb的文件上慢得多呢?
27KB
>>> timeit.timeit('x = len(r.text)', 'from requests import get; r = get("https://cdn.discordapp.com/attachments/280190011918254081/293010649754370048/Journal.170203183244.01.log")', number = 20)
5.78126864130499
Run Code Online (Sandbox Code Playgroud)
1KB
>>> timeit.timeit('x = len(r.text)', 'from requests import get; r = get("https://cdn.discordapp.com/attachments/280190011918254081/293016636288663562/Journal.170109120508.01.log")', number = 20)
0.00036539355403419904
Run Code Online (Sandbox Code Playgroud)
问题是,这个例子在我的开发机器上运行,这是一台普通的工作电脑.运行代码的机器是RaspberryPi,它的速度要慢几个数量级.