我需要找到一个瓶颈,并且需要准确地测量时间.
以下代码段是衡量性能的最佳方法吗?
DateTime startTime = DateTime.Now;
// Some execution process
DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);
Run Code Online (Sandbox Code Playgroud) 我想跟踪我的代码的性能,所以我使用了存储开始和结束时间System.DateTime.Now.我将两者之间的差异作为执行代码的时间.
我注意到差异似乎并不准确.所以我尝试使用一个Stopwatch对象.事实证明,这更加准确.
谁能告诉我为什么Stopwatch比计算开始和结束时间之间的差异更准确System.DateTime.Now?
顺便说一句,我不是说百分之十.我得到了大约15-20%的差异.
显然我能做到DateTime.Now.After- DateTime.Now.Before但必须有更复杂的东西.
任何提示赞赏.
我需要计算用C#编写的代码序列的执行时间.使用DateTime.Now我得到的毫秒字段值不正确.例如:
int start_time, elapsed_time;
start_time = DateTime.Now.Millisecond;
for(int i = 0; i < N_ITER; i++) {
// cpu intensive sequence
}
elapsed_time = DateTime.Now.Millisecond - start_time;
Run Code Online (Sandbox Code Playgroud)
elapsed_time给出负值.
我如何替换DateTime以获得经过时间的实际值?
我需要计算我的WPF应用程序的空闲时间(空闲时间=没有键盘输入,鼠标输入(移动+点击)).到目前为止,我尝试了两种方法,但似乎没有一种方法可行:
还有其他想法吗?或者可能是修改第二个解决方案的方法?
我正在为我的大学制作一个图像隐写术项目.我已完成项目并保留了几种不同的算法来隐藏图像中的数据.
我想问的是,在C#中有什么方法可以通过它找到程序中两点之间的执行/运行时间.例如
//Some Code
//Code to start recording the time.
hideDataUsingAlgorithm();
//Code to stop recording and get the time of execution of the above function.
Run Code Online (Sandbox Code Playgroud)
我想这样做是为了显示简单(耗时较少)和更高效但耗时的算法(使用相同的数据和相同的图像)之间的区别.我有大约10种不同的Color和GrayScale图像算法.
没有多线程因此不会成为问题.Theres只是一个主线程.
我做了一个简单的WindowsFormsApplication,但我遇到了一些困难.在表单中我有10个TextBoxes和一个按钮.该程序的目的是在单击按钮时在每个框中生成不同的数字.这是代码的一部分:
private void button1_Click(object sender, EventArgs e)
{
int p = 0;
int[] array = GeneratingArray();
foreach (Control c in tableLayoutPanel1.Controls)
{
if (c.GetType().ToString() == "System.Windows.Forms.TextBox")
{
c.Text = array[p].ToString();
p++;
}
}
}
public int GeneratingInt()
{
int random;
Contract.Ensures(Contract.Result<int>() > -11, "Array out of Bounds(-10 ; 10)");
Contract.Ensures(Contract.Result<int>() < 11, "Array out of Bounds(-10 ; 10)");
Random gnr = new Random();
random = gnr.Next(20);
return random;
}
public int[] GeneratingArray()
{
int[] array = new int[10];
int random; …Run Code Online (Sandbox Code Playgroud) 我正在写udp组播数据报接收器.
如果我收到数字X的数据报并且数据报"X-1"仍未收到,我应该等待5毫秒(因为UDP不保证数据包的顺序),如果数据报"X-1"仍未收到,我应该恢复.
怎么做?我希望在收到数据包时为数据中的每个接收数据包存储"时间戳".后来我想比较当前时间和时间戳,如果差异超过5毫秒且数据包X-1丢失,我应该恢复.
可能你可以建议另一种算法?
或者如果我很好,我怎么能将"当前时间"转换为"int"或"long"毫秒?我不想使用DateTime.Now对象,因为它包含我不需要的大量垃圾,我需要每秒处理数千次这样的处理.