Using Stopwatch in C#
Stopwatch (Sekundamer) – o’tgan vaqtni o’lchash uhcun
ishlatiladi. Odatda kodni test qilish, optimallashtirish uchun qulay.
Stopwatch sinfi System.Diagnostics namespaseda joylashgan uning daraxtsimon
ko’rinishi quyidagicha:
Stopwatch sinfida quyidagi “xususiyat”(properties)lar
aniqlangan:
-
Elapsed - Umumiy
ketgan vaqtni qaytaradi (Hours, Minutes, Seconds, Milliseconds, Days kapi
maydonlar mavjud)
Stopwatch sinfida quyidagi metodlar
aniqlangan:
Start – Stopwatchni yuklash yoki davom qilidirish uchun
ishlatiladi
Stop – Stopwatchni to’xtatish uchun ishlatiladi
Restart – Stopwatch ob’ektni qiymatlarni nolga
tenglashtiradi va qaytib ‘start’ qiladi
Reset -
Stopwatch ob’ekti qiymatlarini nolga deb oladi
Maslan:
1)
N! ni hisoblash
using System;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplicationStopWatchTest1
{
class Program
{
static long fact(int n)
{
Thread.Sleep(100);//100
millisekund kutish
if (n==0)
return 1;
else
{
return n*fact(n - 1);
}
}
static void Main(string[] args)
{
Console.WriteLine("Stopwatch
C#:");
Stopwatch timer=new Stopwatch();
//N!=1*2*...*N
ni hisoblash
Console.Write(">
N=");
int n=int.Parse(Console.ReadLine());
long result = 0;
timer.Start();
result = fact(n);
timer.Stop();
Console.WriteLine("-------------
Natija -------------------");
Console.WriteLine("{0}! = {1}",n,result);
Console.WriteLine("Hisoblash
uchun ketgan vaqt:{0} soat, {1} min, {2} sek, {3}"+
"
milli sek vaqt ketdi!",timer.Elapsed.Hours,
timer.Elapsed.Minutes,timer.Elapsed.Seconds,timer.Elapsed.Milliseconds);
Console.ReadLine();
}
}
}
Natija:
2) C# Windows Forms Application yordamida “SEKUNDAMER” desturini yaratish
“Sekundamer” dasturini WindowsFormsApp da yaratish uchun shu tipdagi yangi
proyekt yaratamiz, va undagi Form1 formasini quyidagi ko’rinishda quramiz:
“Boshlash” tugmasini – btnStart, “Stop” tugmasini – btnStop, “Reset
qilish” tugmasini btnReset,
Labellarni esa mos ravishda lblStopWatchFullValue va lblStopWatchInMilliSeconds
deb nomlaymiz.
Bizga “Timer” komponenti kerak bo’ladi, uni C# standart komponentlari
ichidan (odatda “Components” bo’limida joylashadi) o’rnatamiz.
Form1.cs sinfi kodi quyidagicha bo’ladi:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplicationStopWatchTest1
{
public partial class Form1 : Form
{
private Stopwatch sw;
public Form1()
{
InitializeComponent();
sw=new Stopwatch();
}
private void btnStart_Click(object sender, EventArgs e)
{
sw.Start();
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
sw.Stop();timer1.Stop();
}
private void btnReset_Click(object sender, EventArgs e)
{
sw.Reset();
lblStopWatchFullValue.Text = "00:00:00:00";
lblStopWatchInMilliSeconds.Text = "0 Millisekund";
}
private void timer1_Tick(object sender, EventArgs e)
{
lblStopWatchFullValue.Text = string.Format("{0}:{1}:{2}:{3}", sw.Elapsed.Hours,
sw.Elapsed.Minutes, sw.Elapsed.Seconds,
sw.Elapsed.Milliseconds);
lblStopWatchInMilliSeconds.Text = string.Format("{0} Millisekund",
sw.ElapsedMilliseconds);
}
}
}
Dasturni ishga tushirib, “Boshlash" tugmasini bosganda quyidagi natijani
olishimiz mumkin:
Stopwatch dan net dasturlashda(socketlar bilan ishlaganda), DB ga
yuborilayatgan query(zopros)larning bajarilishi uchun ketgan vaqtni aniqlashda
foydalanish mumkin
Masalan quyidagi rasmda “DataWhare House” ga bo’layotgan zaproslar va
ularning bajarilishi uchun ketgan vaqtni service orqali monitoring qilish
jarayoni qisman ko’rsatilgan. Ushbu jarayonda Thread va Stopwatchdan
foydalanish mumkin.
Useful LINKS:
- http://msdn.microsoft.com/ru-ru/library/system.diagnostics.stopwatch(v=vs.110).aspx
- http://tipsandtricks.runicsoft.com/CSharp/PerformanceMeasuring.html
- http://www.dotnetfunda.com/articles/show/907/stopwatch-application-using-csharp
No comments:
Post a Comment
Note: only a member of this blog may post a comment.