0
\$\begingroup\$

So, there is a big jagged string array (~ [120] [1 000 000]) here that represents data from an excel worksheet ( columns / rows).

Task: We have to append items at the beginning of each inner array.

I have a solution, but I am not sure about performance.

private string[][] AddField(string[][] exportDataFieldValues, string linkDisplayFieldValue)
    {
        var newExportDataFieldValues = new List<string[]>();
        foreach (var row in exportDataFieldValues)
        {
            var cellsRange = row.ToList();
            cellsRange.Insert(0, linkDisplayFieldValue);

            newExportDataFieldValues.Add(cellsRange.ToArray());
        }

        return newExportDataFieldValues.ToArray();
    }

Q: As you can see, my solution contains a lot of type conversions, I believe there is a more effective solution. Сan you provide a more effective solution?

\$\endgroup\$
1
  • 6
    \$\begingroup\$ I would ask first why you have a jagged array at all? How do you create it? What you are doing is super inefficient. Instead if optimizing this method I think it might be better to start at the beginning and not somewhere in the middle of the process. So could you share more code? \$\endgroup\$
    – t3chb0t
    Commented Jul 12, 2018 at 16:03

2 Answers 2

3
\$\begingroup\$

I guess your code will behave very bad performance wise.
I would suggest to create the cellsRange object first as a List<whatever> and add 0,linkDisplayFieldValue first and then call AddRange() on cellsRange passing row.ToList() as parameter.

\$\endgroup\$
5
  • \$\begingroup\$ var newExportDataFieldValues = new string[exportDataFieldValues.Length][]; \$\endgroup\$
    – AllmanTool
    Commented Jul 12, 2018 at 16:07
  • \$\begingroup\$ for (var row = 0; row < exportDataFieldValues.Length; row++) \$\endgroup\$
    – AllmanTool
    Commented Jul 12, 2018 at 16:07
  • \$\begingroup\$ { newExportDataFieldValues[row] = new[] { linkDisplayFieldValue }.Concat(newExportDataFieldValues[row]).ToArray(); } \$\endgroup\$
    – AllmanTool
    Commented Jul 12, 2018 at 16:08
  • \$\begingroup\$ Is it will be more effective? \$\endgroup\$
    – AllmanTool
    Commented Jul 12, 2018 at 16:08
  • \$\begingroup\$ Thansk for your help \$\endgroup\$
    – AllmanTool
    Commented Jul 12, 2018 at 16:08
1
\$\begingroup\$

You could model your data as an array of List, and encapsulate it in a class, such that all access to the data is only via the class properties and methods.

When initializing this class, reverse the columns to populate the lists. Then to logically add an item to the front, you can instead append to the end of the list, which is quite efficient.

The members of this class should abstract away that fact that the columns are actually stored in reverse order and compute the actual index of the column by subtracting from the number of items in the list. (List.Count() - Idx - 1).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.