Question: When generating a matrix barcode, it takes 1 minute per 10000 files .That's how to generating a matrix barcode for big quantities in less time.

[Here](http://www.mediafire.com/file/ciadoloynverba7/library_used_for_datamatrix.txt) is the library used to encoder data.

My code for button generating:

```
Class1 CLS = new Class1();
DataTable dt = CLS.ShowalldataSerial(textBox4.Text);

for (int i = 0; i <= Convert.ToInt32(textBox1.Text); i++)
{
    Serial = SRL.Rnd().ToString();
    txt = "UserID" + dt.Rows[0][0] + "FirmName" + dt.Rows[0][1] + "OrderNo" + dt.Rows[0][2] + "BtachNo" + dt.Rows[0][3] + "Quantity" + dt.Rows[0][4] + "ProductName" + dt.Rows[0][5] + "SerialNo" + Serial;

    dm.DM(txt, Color.FromName(comboBox1.SelectedItem.ToString()), Color.White).Save(root + "\\" + Serial + ".emf", System.Drawing.Imaging.ImageFormat.Emf);

}
MessageBox.Show("Records generated success ");

```

When creating 10000 in `textbox1` it take 1 minute. If I write 200000 in `textbox1` it takes 20 minutes.

The code is working without any problems and gives me results that I need, but it is slow. Can someone help me speed it up?

Answer: 
1. Strings are immutable so whenever you're concatenating them you're effectively creating new instances every time, this can slow down performance a lot, instead use `StringBuilder` and it's method `Append()` to build strings.
2. Avoid duplicate operations per each cycle unless it's necessary, such as `Convert.ToInt32(textBox1.Text)` convert the value to `int` before the for loop and save the value in variable, than later use the variable instead of converting the value every time. Another example is `comboBox1.SelectedItem.ToString()`.
I don't see you using the `i` variable inside your loop? If it's just for counting you could perhaps rewrite the loop to:

```
var counter = Convert.ToInt32(textBox1.Text);

while(counter-- >= 0)
{
    //Code goes here...
}

```

I also see you always use the row at index `0`, is this intended or is that where you meant to take the "i-th" row?

* Always use `0`: place the row in a variable to reuse, and not re-access it every time

```
while(counter-- >= 0)
{
    var row = dt.Rows[0];
    //Access the row:
    var cell = row[0];
}

```
* Take i-th row: use the for-loop and the indexer

```
var counter = Convert.ToInt32(textBox1.Text);

for (int i = 0; i <= counter; i++)
{
    var row = dt.Rows[i];
}

```

Another tip is to use a formatted string to build the result:

```
while(counter-- >= 0)
{
    Serial = SRL.Rnd().ToString();
    var row = dt.Rows[0];
    var formatted = $"UserID{row[0]}FirmName{row[1]}OrderNo{row[2]}BtachNo{row[3]}Quantity{row[4]}ProductName{row[5]}SerialNo{Serial}";
}

```

With all tips, including from other answers your code would look like this:

```
var CLS = new Class1();
var dt = CLS.ShowalldataSerial(textBox4.Text);
var counter = Convert.ToInt32(textBox1.Text);
var selectedColor = Color.FromName(comboBox1.SelectedItem.ToString());

while(counter-- >= 0)
{
    Serial = SRL.Rnd().ToString();
    var row = dt.Rows[0];
    var txt = $"UserID{row[0]}FirmName{row[1]}OrderNo{row[2]}BtachNo{row[3]}Quantity{row[4]}ProductName{row[5]}SerialNo{Serial}";
    dm.DM(txt, selectedColor, Color.White).Save($"{root}\\{Serial}.emf", System.Drawing.Imaging.ImageFormat.Emf);
}

```