Как правильно поставить таймер? - C#
Формулировка задачи:
Здравствуйте! У меня есть программа, которая решает СЛАУ методом Гаусса. Мне нужно вывести время за которое она решила СЛАУ. Как это правильно сделать, подскажите, пожалуйста!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Timers;
namespace Gausse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string Gauss(DataGridView matrix)
{
int rows = matrix.RowCount;
int cols = matrix.ColumnCount;
double[,] array = new double[rows, cols];//массив
for (int i = 0; i < rows; i++)//по строкам
for (int j = 0; j < cols; j++)//по столбцам
array[i, j] = double.Parse(matrix.Rows[i].Cells[j].Value.ToString());
for (int i = 0; i < rows - 1; i++)
for (int j = i + 1; j < rows; j++)
{
double b = (array[j, i] / array[i, i]);
for (int a = 0; a < cols; a++)
if (array[j, a] != 0)
{
array[j, a] = double.Parse((array[j, a] - array[i, a] * b).ToString());
}
}
for (int i = rows - 1; i > 0; i--)
for (int j = i - 1; j >= 0; j--)
{
double buff = (array[j, i] / array[i, i]);
for (int a = 0; a < cols; a++)
if (array[j, a] != 0)
{
array[j, a] = double.Parse((array[j, a] - array[i, a] * buff).ToString());
}
}
string result = "";
for (int i = 0; i < rows; i++)
result += "x" + "("+i.ToString()+")" + "=" + Math.Round(array[i, cols - 1] / array[i, i], 2).ToString() + "; ";
return result;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 0;
dataGridView1.RowCount = 0;
dataGridView1.ColumnCount = int.Parse(numericUpDown1.Value.ToString()) + 1;
dataGridView1.RowCount = int.Parse(numericUpDown1.Value.ToString());
for (int i = 0; i < dataGridView1.ColumnCount - 1; i++)
dataGridView1.Columns[i].HeaderText = "a" + (i + 1).ToString();
dataGridView1.Columns[dataGridView1.ColumnCount - 1].HeaderText = "b";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Gauss(dataGridView1);
}Решение задачи: «Как правильно поставить таймер?»
textual
Листинг программы
private void button1_Click(object sender, EventArgs e)
{
long t1 = DateTime.Now.Ticks;
textBox1.Text = Gauss(dataGridView1);
long t2 = DateTime.Now.Ticks;
long difference = (t2 - t1)/10000;
контрол_для_вывода_времени.Text = "Потрачено "+ difference.ToString() + " миллисекунд.";
}