Hi,

I written this code to calculate the clusters with the points taken from a datagridview.

It works fine for small number of data points. But taking lot of time(nearly 15 mins) to run for 1000 rows and 5 columns in a datagridview.

Is there a way to make it faster?

private void button2_Click(object sender, EventArgs e)
        {
            int[] s = new int[10000];
            double[] k1 = new double[100];
            double h = 0;
            Dictionary<double, List<double>> d1 = new Dictionary<double, List<double>>();
            List<double> l = new List<double>();
            string d = "";
            int ch;
            double f = 0.0001;
            Random Rnd = new Random();
            int k = int.Parse(textBox4.Text);
            double old_error, error = double.MaxValue;
            double q = 0, v = 0, o = 0;
            double[,] m1 = new double[dataGridView1.RowCount, 8];
            double[,] c1 = new double[dataGridView1.RowCount, 8];
            int u = int.MaxValue;
            int[] counts = new int[1000000];
            a = new double[dataGridView1.RowCount, 8];
            int j1 = 0;
            t = new double[k, 7];
            //  MessageBox.Show(dataGridView1.RowCount.ToString());
            
            do
            {
                richTextBox4.Clear();
                for (int i = 0; i < k; i++)
                {
                    counts[i] = 0;

                    for (int j = 0; j < 6; j++)
                    {
                        c1[i, j] = 0;
                    }
                }
                old_error = error; error = 0;
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    //  textBox1.Text = dataGridView1.Rows[i].Cells[0].ToString();
                    for (int j = 0; j < 6; j++)
                    {
                        //   MessageBox.Show(dataGridView1.Rows[i].Cells[j].Value.ToString());
                        a[i, j] = double.Parse(dataGridView1.Rows[i].Cells[j].Value.ToString());
                        richTextBox2.AppendText(a[i, j].ToString() + ",");
                    }
                    richTextBox2.AppendText("\n");
                }


                richTextBox1.Clear();

                for (int i = 0; i < k; i++)
                {
                    ch = Rnd.Next(0, dataGridView1.RowCount);
                    try
                    {

                        for (int j = 0; j < 6; j++)
                        {

                            t[i, j] = double.Parse(dataGridView1.Rows[ch].Cells[j].Value.ToString());
                            // MessageBox.Show("2 - "+dataGridView1.Rows[ch].Cells[j].Value.ToString());
                            if (j != 0)
                            {
                                richTextBox1.AppendText(ch + ", " + t[i, j].ToString() + "\n");
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                for (int j = 0; j < dataGridView1.RowCount - 1; j++)
                {
                    for (int i = 0; i < k; i++)
                    {
                        for (int m = 1; m < 6; m++)
                        {
                            q = (Math.Pow((t[i, m] - a[j, m]), 2));
                              //MessageBox.Show("res - " + q.ToString());
                            v = v + q;


                            q = 0;
                        }
                        // MessageBox.Show("total - " + v.ToString());
                        o = Math.Sqrt(v);
                        //  MessageBox.Show("res1 - " + o.ToString());
                        if (h > o)
                        {
                            s[j] = i;
                            h = o;

                        }

                        v = 0;
                        o = 0;
                    }
                    richTextBox4.AppendText(s[j] + "-");
                    for (int m = 0; m < 6; m++)
                    {
                        c1[s[j], m] = a[j, m];
                        counts[s[j1]]++;
                    }
                    richTextBox4.AppendText((c1[s[j], 0]).ToString());
                    richTextBox4.AppendText("\n");
                    error += h;
                    h = double.MaxValue;
                }
                for (int i = 0; i < k; i++)
                { /* update all centroids */
                    for (int j = 1; j < 6; j++)
                    {
                        t[i, j] = (counts[i] != 0) ? c1[i, j] / counts[i] : c1[i, j];
                    }
                }

            } while (Math.Abs(error - old_error) > f);
           
        }

Why counts[i] = 0; it's initialized by default.
and this too c1[i, j] = 0;

This is done inorder to free the array during the loop.

new it like counts = new int[100000]; time to initialize it by for loop 430024
time to initialize it by new 10001

new it like counts = new int[100000]; time to initialize it by for loop 430024
time to initialize it by new 10001

Hi

I removed it. But still its the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.