Why is reading lines from stdin much slower in C++ than Python?

前端 未结 10 2062
野趣味
野趣味 2020-11-22 03:06

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Pyt

10条回答
  •  猫巷女王i
    2020-11-22 03:59

    Well, I see that in your second solution you switched from cin to scanf, which was the first suggestion I was going to make you (cin is sloooooooooooow). Now, if you switch from scanf to fgets, you would see another boost in performance: fgets is the fastest C++ function for string input.

    BTW, didn't know about that sync thing, nice. But you should still try fgets.

提交回复
热议问题