Why is C + + reading files slower than Python?

Source: Internet
Author: User
The problem is, I have a folder that has about 100,000 rows of data, and then I use C + + and Python to figure out how many rows of data are in line with the requirements. Because I don't know why, C + + and Python can only count more than 7,000 of these lines, and then I use the file pointer location to solve the problem, and then C + + and Python write two programs are able to run and calculate. But I found that C + + ran the result in 29 seconds, and Python only takes 7 seconds. Why is that?
ps:c++ I use the Getline function, and Python I use the ReadLines function.
Another problem is that in another file with unequal length in each line, C + + pointer positioning is not always as accurate as python to the end of the file, the file pointer and getline function collocation How to use ...? Ask God for great God ... Crying ...

Reply content:

Because C + + has "a historical burden of compatibility with c". "

By default std::cin is synchronized with stdin buffers after each operation so that iostream and stdio are mixed. Switching off this behavior can significantly improve performance:
std::ios_base::sync_with_stdio(false);
Even if Liu Xiang ran a step to stop, and then run a step further stop, nor I run 10,000 meters in a row fast ... Landlord you believe it or not I think the comparison with getline should be the Python readline function, not the ReadLines function. In the case of optimization, there is nothing more than a compromise between time and space. Getline returns one row at a time and uses only a single row of memory. And ReadLines reads 100,000 lines at a time, and then one line to you. C + + spends more time on the memory he spends less. Study the
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.