I have profiled a complex C ++ program on Linux using cachegrind. Surprisingly, it turned out that the bottleneck of my program was not in any sorting or computational method ... it was while reading the input.
Here is a screenshot of cachegrind if I misinterpret the profiling results ( see scanf() ):

I hope I am right in saying that scanf() takes up 80.92% of my working time.
I am reading input using cin >> int_variable_here , for example:
std::ios_base::sync_with_stdio (false); // Supposedly makes I/O faster cin >> NumberOfCities; cin >> NumberOfOldRoads; Roads = new Road[NumberOfOldRoads]; for (int i = 0; i < NumberOfOldRoads; i++) { int cityA, cityB, length; cin >> cityA; //scanf("%d", &cityA); // scanf() and cin are both too slow cin >> cityB; //scanf("%d", &cityB); cin >> length; //scanf("%d", &length); Roads[i] = Road(cityA, cityB, length); }
If you have not noticed any problems with this input input code, could you recommend a faster way to read the input? I'm thinking of trying getline() (working on it while I wait for answers). I assume that getline () might work faster because it needs to do less conversion and it analyzes the stream less than the total number of times (just my guess, although I will eventually have to parse the strings as integers).
What I mean by “too slow” is that it is part of a larger homework that expires after a certain period of time (I think it is 90 seconds). I'm pretty sure the bottleneck is here because I intentionally commented on the bulk of the rest of my program, and it is still timed. I don’t know what test examples the instructor runs through my program, but it must be a huge input file. So what can I use to quickly read input?
The input format is strict: 3 integers, separated by one space for each line, for many lines:
Input Example:
7 8 3 7 9 2 8 9 1 0 1 28 0 5 10 1 2 16
I need to make Road from integers on each line.
It is also not recommended that the input be redirected to my program for standard input ( myprogram < whatever_test_case.txt ). I do not read a specific file. I just read from cin .
Update
Using the Glory method:
Reading the input seems to take less time, but its timeout (maybe not due to data input anymore). The Slava method is implemented in Road() ctor (2 from main ). So now it takes 22% of the time, not 80%. I am thinking about optimizing SortRoadsComparator() , as it is called 26,000,000 times.

Comparator Code:
// The complexity is sort of required for the whole min() max(), based off assignment instructions bool SortRoadsComparator(const Road& a, const Road& b) { if (a.Length > b.Length) return false; else if (b.Length > a.Length) return true; else { // Non-determinism case return ( (min(a.CityA, a.CityB) < min(b.CityA, b.CityB)) || ( (min(a.CityA, a.CityB) == min(b.CityA, b.CityB)) && max(a.CityA, a.CityB) < max(b.CityA, b.CityB) ) ); } }
Using the Entroflept Method

Given the decision
I am going to consider this problem because the bottleneck is no longer used for reading. The Glory Method was the fastest for me.