這確實應該是一個評論,但不管怎麼說。
請注意,有一個公認的Python 2解決方案here,運行時45.77s,所以很顯然可能。我認爲你是Python 3慢速I/O的受害者(看起來他們正在使用3.1.2)。在一個200萬行的輸入文件中(這個文件沒有任何可以被整除的數字):在很多情況下沒有多少區別),在修改爲與2和3兼容的代碼版本中,我得到:
~/coding$ time python2.6 enormread.py < sample.txt
0
real 0m3.971s
user 0m3.712s
sys 0m0.256s
~/coding$ time python2.7 enormread.py < sample.txt
0
real 0m2.637s
user 0m2.428s
sys 0m0.204s
~/coding$ time python3.2 enormread.py < sample.txt
0
real 0m10.412s
user 0m10.065s
sys 0m0.344s
~/coding$ time ~/sys/Python-3.3.0a2/python enormread.py < sample.txt
0
real 0m6.776s
user 0m6.336s
sys 0m0.436s
~/coding$ time pypy enormread.py < sample.txt
0
real 0m2.211s
user 0m1.948s
sys 0m0.028s
要扔@ AGF的(sum(not int(line) % t for line in sys.stdin[.buffer]))
混進去:
~/coding$ time python2.7 enormfast.py < sample.txt
0
real 0m1.454s
user 0m1.436s
sys 0m0.016s
~/coding$ time python3.2 enormfast.py < sample.txt
0
real 0m2.243s
user 0m2.228s
sys 0m0.012s
來源
2012-04-25 18:44:09
DSM
可以粘貼輸入? – 2012-04-25 18:15:46
@AshwiniChaudhary:你的意思是全部20MB?問題被稱爲「巨大的輸入測試」 – 2012-04-25 18:17:45
@agf是的。程序必須從標準輸入讀取並寫入標準輸出。 – 2012-04-25 18:23:28