在调查线程时我发现了令人惊讶的python行为为什么在C++中读取stdin的行比Python要慢得多?.
如果我从该线程运行简单的python代码
#!/usr/bin/env python
from __future__ import print_function
import time
import sys
count = 0
start_time = time.time()
for line in sys.stdin:
count += 1
delta_sec = time.time() - start_time
if delta_sec >= 0:
lines_per_sec = int(round(count/delta_sec))
print("Read {0:n} lines in {1:.2f} seconds. LPS: {2:n}".format(count, delta_sec, lines_per_sec))
Run Code Online (Sandbox Code Playgroud)
它的工作速度为11.5M LPS,当我将整个脚本分解为单个功能时
#!/usr/bin/env python
from __future__ import print_function
import time
import sys
def test(input):
count = 0
start_time = time.time()
for line in input:
count += 1
delta_sec = time.time() - …Run Code Online (Sandbox Code Playgroud)