Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

performance - Fastest stdin/out IO in python 3?

I've been solving a few problems on SPOJ.pl using python 3.1.2 and some peoples fast result on simple problems makes me wonder if there is a faster way to handle input and output.

I've tried using

input()
print()

and

sys.stdin.readline()
sys.stdout.write()

or rather

for line in sys.stdin:
    #Handle input
    sys.stdout.write(output)

to process each line. I've also tried to collect all output in lists and print all at once when everything is processed.

But all of these produce similar execution times.

Is there any faster way to handle input and output from stdin/out?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following will probably be fastest:

  1. Read all the input at once using os.read(0, some_big_enough_number).

  2. Process the output, collecting the results in a list results.

  3. Write all the output at once using os.write(1, "".join(results)).

I remember one case where I noticed that os.read() and os.write() are sometimes faster than using Python I/O, but I don't remember the details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...