如何加快速度 [英] How to speed this up
本文介绍了如何加快速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,这是我上周发布的一个问题的后续内容 - 以下代码在我的本地局域网上发现了一个媒体服务器,它可以工作但速度非常慢,我想知道如何加速它或者以不同的方式执行
Hi all, this is a follow on from a question I posted last week - the following code discovers a media server on my local lan , it works but is very slow and I would like some advice as to how I can either speed it up or do it differently
void ScanForServer()
{
IPEndPoint ReceiveIP = null;
UdpClient listener = null;
Byte[] RequestData = null;
byte[] BytesReceived = null;
int UDPPort = 3483;
try
{
ReceiveIP = new IPEndPoint(IPAddress.Any, UDPPort);
listener = new UdpClient(this.UDPPort);
// RequestData will contain a server request in the correct format
// ReceivIP will contain the ip address of a discovered server.
RequestData = listener.Receive(ref ReceiveIP);
// Which I then send back as a request
listener.Send(RequestData, RequestData.Length, ReceiveIP);
// This will return server information such as the tcp port.
BytesReceived = listener.Receive(ref ReceiveIP);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (listener != null && listener.Client.Connected)
listener.Close();
}
}
因为我说它工作得很好但很慢(可能是30秒左右) )
as I say it works perfectly but is VERY SLOW ( can be 30 seconds or so )
推荐答案
正如戴夫所说:你的代码正在等待响应,而且无法加速。
然而,它可能会值得使用秒表类来准确计算出它的进展情况:
As Dave says: your code is waiting for responses, and that can't be speed up.
However, it might be worth using the Stopwatch class to work out exactly where it is going slowly:
void ScanForServer()
{
IPEndPoint ReceiveIP = null;
UdpClient listener = null;
Byte[] RequestData = null;
byte[] BytesReceived = null;
int UDPPort = 3483;
Stopwatch s1 = new Stopwatch();
Stopwatch s2 = new Stopwatch();
Stopwatch s3 = new Stopwatch();
Stopwatch s4 = new Stopwatch();
Stopwatch s5 = new Stopwatch();
Stopwatch s6 = new Stopwatch();
try
{
s1.Start();
ReceiveIP = new IPEndPoint(IPAddress.Any, UDPPort);
s1.Stop();
s2.Start();
listener = new UdpClient(this.UDPPort);
s2.Stop();
s3.Start();
// RequestData will contain a server request in the correct format
// ReceivIP will contain the ip address of a discovered server.
RequestData = listener.Receive(ref ReceiveIP);
s3.Stop();
s4.Start();
// Which I then send back as a request
listener.Send(RequestData, RequestData.Length, ReceiveIP);
s4.Stop();
s5.Start();
// This will return server information such as the tcp port.
BytesReceived = listener.Receive(ref ReceiveIP);
s5.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
s6.Start();
if (listener != null && listener.Client.Connected)
listener.Close();
s6.Stop();
}
Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}",
s1.ElapsedMilliseconds,
s2.ElapsedMilliseconds,
s3.ElapsedMilliseconds,
s4.ElapsedMilliseconds,
s5.ElapsedMilliseconds,
s6.ElapsedMilliseconds);
}
您的代码中可能没有任何关于它的信息,但它可以帮助您了解哪个部分是迟缓的。
There probably isn't anything you can do about it in your code, but it may help you to know which part is sluggish.
>
这篇关于如何加快速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文