'Why is my c++ sorting function so much slower than my c# sorting function?

I'm very new to c++, but I do have experience with other object oriented programming languages.

I'm attempting to alphabetically sort the lines in a file I have, which has roughly 5300 lines. I originally wrote the program in c++ for practice, but was curious to see how it would perform against my main language, c#.

To my surprise, the same sorting algorithm which takes my c++ function 18-20 seconds to execute, finishes in less than 3 seconds in c#.

Given that I am very new to c++ (and not a very experienced programmer in general), I am sure this must be an error in the way I wrote something.

With all that being said, I am aware that there are quicker sorting methods to use. However, both programs are using the same algorithm so I don't understand the reason for the large performance gap.

I will note that I have tried converting the data to an array instead of a vector, but sorting the array was only consistently about 3 seconds faster (about 15 seconds total instead of 18).

What am I doing wrong? Any/all help is appreciated!

Below is the c++:

void select_sort_alphabetical(std::vector<std::string> _vector)
{
    std::cout << "<< SORTING... >>" << "\n\n";

    int char_index, i, j, size = _vector.size(), loop_iterations = 0;
    char char1, char2;
    std::string temp;
    
    // Iterate through all lines
    for (i = 0; i < (size - 1); i++)
    {
        for (j = (1 + i); j < size; j++)
        {
            char_index = 0;

            char1 = _vector[i][char_index]; // Getting first character of each line
            char2 = _vector[j][char_index];
            
            // While the letters to be compared are the same, move onto the next character
            while (char1 == char2)
            {
                char_index++;
                char1 = _vector[i][char_index]; // Setting chars to the next characters in each line
                char2 = _vector[j][char_index];
            }
            // Once the characters are different - if line x.ascii_code greater than line x+1.ascii_code...
            if (_vector[i][char_index] > _vector[j][char_index]) // comparing characters
            {
                // Swapping places
                temp = _vector[i];
                _vector[i] = _vector[j];
                _vector[j] = temp;
            }
            loop_iterations++;
        }
    }

    //print_lines_from_vect(_vector);

    // Clearing contents of vector and freeing up memory (trying to, anyway)
    _vector.clear();
    _vector.shrink_to_fit();

    std::cout << "\nIterations: " << loop_iterations << "\n";
}

and here is the c#:

public static string[] select_sort_alphabetical(string[] lines, ref int loop_iterations)
        {
            Console.WriteLine("<< SORTING... >>");

            // Iterate through all lines
            for (int i = 0; i < (lines.Length - 2); i++)
            {
                for (int j = (1 + i); j < (lines.Length); j++)
                {
                    int char_index = 0;

                    char char1 = lines[i][char_index]; // Getting first character of each line
                    char char2 = lines[j][char_index];

                    // While the letters to be compared are the same, move onto the next character
                    while (char1 == char2)
                    {
                        char_index++;
                        char1 = lines[i][char_index];
                        char2 = lines[j][char_index];
                    }
                    // Once the characters are different - if line x.ascii_code greater than line x+1.ascii_code...
                    if (lines[i][char_index] > lines[j][char_index]) // comparing characters
                    {
                        // Swapping places
                        string temp = lines[i];
                        lines[i] = lines[j];
                        lines[j] = temp;
                    }
                    loop_iterations++;
                }
            }
            return lines;
        }


Solution 1:[1]

One reason your algorithm would be slower, without taking in account other differences between languages, is swapping lines:

// Swapping places
string temp = lines[i];
lines[i] = lines[j];
lines[j] = temp;
  • in c#, string temp = original means temp and original point to the same string. modifying each will reflect in the other.
  • in c++, string temp = original means temp is a new string. Modifying one will not modify the other.

C++ provides move class members, which allow new objects to "steal" the resources of the original object.

std:.swap, in order to swap objects, make something like

string temp = steal(lines[i]);
lines[i] = steal(lines[j]);
lines[j] = steal(temp);

This is a simplification of the real mechanism.

Btw, if you use swap, you will see far faster debug, because that line swapping is a big cost in your algorithm.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1