Ich habe überraschend Python Verhalten entdeckt, während Why is reading lines from stdin much slower in C++ than Python? Thread zu untersuchen hatte.Warum Mesh Python Code langsamer als zerlegt?
Wenn ich laufe einfach Python-Code aus diesem Thread
#!/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))
es mit Geschwindigkeit 11.5M LPS arbeitet, und wenn ich das ganze Skript in einzelne Funktion
#!/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() - 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))
if __name__ == "__main__":
test(sys.stdin)
Code Geschwindigkeiten von bis zu 23M zersetzen LPS.
Warum dieses einfache Refactoring macht meinen Code 2-mal schneller?
Ich habe meine Tests mit python2.7 auf Ubuntu 13.10 laufen.
Einheimische vs Globals – SethMMorton
versuchen 'Eingang = für Zeile in Eingabe sys.stdin: ...' in Ihrem ersten Skript –
diese Methode beschleunigt erste Skript –