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