Fastest stdin/out IO in python 3?

后端 未结 3 730
Happy的楠姐
Happy的楠姐 2021-01-01 15:28

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

3条回答
  •  温柔的废话
    2021-01-01 16:00

    SPOJ lets you choose among a variety of programming languages. Are you comparing your execution time to other solutions written in other programming languages?

    Just for fun, I submitted the following solutions to the first problem (codename TEST) to compare run-times.

    C++ solution (G++ 4.3.2)

    #include 
    int main ( int, char** )
    {
         for ( int number=0; (std::cin >> number) && (number != 42); ) {
             std::cout << number << std::endl;
         }
    }
    

    See the submission.

    Python (2.5) solution

    import sys
    for line in sys.stdin:
        number = int(line)
        if number == 42:
            break
        print number
    

    See the submission.

    Conclusion

    I'm not 100% sure that this gets the absolute best performance in either languages, but there's not so much code in there to optimize.

    I get time 0.00 measurement for the C++ and 0.04 measurement for the Python code. Assuming the sequence of numbers submitted to both programs is the same, I think comparison of run-times against solutions in other languages is almost meaningless (see next paragraph).

    Now, this is only true for simple problems. Most advanced problems require choosing the right algorithm for the problem and choosing the wrong one has drastic consequences. In those cases, carefully crafted Python solutions might still be slower than carefully crafted C++ solutions but the good Python solution will beat a naïve solution written in any other language.

提交回复
热议问题