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

前端 未结 10 2004
野趣味
野趣味 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条回答
  •  滥情空心
    2020-11-22 04:04

    In your second example (with scanf()) reason why this is still slower might be because scanf("%s") parses string and looks for any space char (space, tab, newline).

    Also, yes, CPython does some caching to avoid harddisk reads.

提交回复
热议问题