for循环次数比较多,如何提高速度?

chankite 2012-02-24 06:17:36
我需要做的就是在for操作里面,每次循环ping不同IP,速度很慢,如何提高?麻烦贴出code,谢谢啦。
...全文
1325 29 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
hetengfei_ 2012-02-25
  • 打赏
  • 举报
回复
总之,只要思路没错,调试几次,并改进,总会可以的。
threenewbee 2012-02-25
  • 打赏
  • 举报
回复
等下周三VS11 Beta出来了,你可以试下C# 4.5的异步语法。

for (int i = 0; i < n; i++)
{
int result = await pingip(ip[i]);
}
hetengfei_ 2012-02-25
  • 打赏
  • 举报
回复
代码同上:
int[] sResult = new int[255];//声成全局变量
for (int i = 0; i < 255; i++)
{
sResult[i]=0;//数据初始化
}
for (int i = 0; i < 255; i++)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
Thread thisThread = new Thread(pts);
string strIP = strGateWay + i.ToString();

//当前需要Ping的IP
object[] sendPara = new object[2];
sendPara[0] = strIP;
sendPara[1]= i;

thisThread.Start(strIP);

}
public void threadPing(object ip)
{
int index = (int)((object[])ip)[1];
string strIp = (string)((object[])ip)[0];;
if (IsPingIP(strIP))
{
sResult[index]=1;
}
else
{
sResult[index]=-1;
}
}

// 结果,
//数组中的 数据为-1 :表示失败;
//数组中的 数据为 0 :表示线程还在睡觉中,--ping 还没有返回;
//数组中的 数据为 1 :表示拼成功;
//
//这个,如果 sResult 中还有0 ,那你可以等一下,即用一个线程sleep一下,等到数组中没有0为止!
hetengfei_ 2012-02-25
  • 打赏
  • 举报
回复
那改一改吧,我用bool 的,声成int 了


int[] sResult = new int[255];//声成全局变量
for (int i = 0; i < 255; i++)
{
sResult[i]=0;//数据初始化
}
for (int i = 0; i < 255; i++)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
Thread thisThread = new Thread(pts);
string strIP = strGateWay + i.ToString();

//当前需要Ping的IP
object[] sendPara = new object[2];
sendPara[0] = strIP;
sendPara[1]= i;

thisThread.Start(strIP);

}
public void threadPing(object ip)
{
int index = (int)((object[])ip)[1];
string strIp = (string)((object[])ip)[0];;
if (IsPingIP(strIP))
{
sResult[index]=1;
}
else
{
sResult[index]=-1;
}
}

// 结果,
//数组中的 数据为-1 :表示失败;
//数组中的 数据为 0 :表示线程还在睡觉中,--ping 还没有返回;
//数组中的 数据为 1 :表示拼成功;
//
//这个,如果 sResult 中还有0 ,那你可以等一下,即用一个线程sleep一下,等到数组中没有0为止!
xwguan 2012-02-25
  • 打赏
  • 举报
回复
楼主做这个程序是想找代理服务器????
chankite 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 amokall 的回复:]

引用 26 楼 chankite 的回复:

引用 25 楼 amokall 的回复:

管for循环屌事,如果换成这样,你试一下,会不会更慢
C# code

for (int i=0;i<10;i++)
{
Thread.Sleep(一万年);
}


那你说说有什么好办法?

我的意思是:慢不是for的原因,时间是费在for里的代码处理上,你要在代码逻辑上突破……
[/Quote]

嗯,的确ping的效率不好。你有什么方法去更快实现这个。
amokall 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 chankite 的回复:]

引用 25 楼 amokall 的回复:

管for循环屌事,如果换成这样,你试一下,会不会更慢
C# code

for (int i=0;i<10;i++)
{
Thread.Sleep(一万年);
}


那你说说有什么好办法?
[/Quote]
我的意思是:慢不是for的原因,时间是费在for里的代码处理上,你要在代码逻辑上突破
chankite 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 amokall 的回复:]

管for循环屌事,如果换成这样,你试一下,会不会更慢
C# code

for (int i=0;i<10;i++)
{
Thread.Sleep(一万年);
}
[/Quote]

那你说说有什么好办法?
amokall 2012-02-25
  • 打赏
  • 举报
回复
管for循环屌事,如果换成这样,你试一下,会不会更慢

for (int i=0;i<10;i++)
{
Thread.Sleep(一万年);
}

笑是我的药 2012-02-25
  • 打赏
  • 举报
回复
用多线程就Ok了
youzelin 2012-02-25
  • 打赏
  • 举报
回复
.NET 4.0 的 Parallel.For/Foreach
hetengfei_ 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 chankite 的回复:]
thisThread.Start的是sendPara 不是strIP
[/Quote]

不好意思,我没有测试,只是找个多线程的例子改改 而已
chankite 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 hetengfei_ 的回复:]

代码同上:
C# code
int[] sResult = new int[255];//声成全局变量
for (int i = 0; i < 255; i++)
{
sResult[i]=0;//数据初始化
}
for (int i = 0; i < 255; i++)
{
ParameterizedThreadStart pts = new ParameterizedThread……
[/Quote]

这代码您测试过了吗?我测试不行,全部变成成功了。

thisThread.Start的是sendPara 不是strIP
chankite 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hetengfei_ 的回复:]

C# code

int[] sResult = new int[255];//声成全局变量
for (int i = 0; i < 255; i++)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
Thread thisThread = new Thread(pts);
……
[/Quote]

threadPing函数内无法强制类型噢。
int index = (int)((object[])ip)[1];

这一句。
zzzzv0 2012-02-24
  • 打赏
  • 举报
回复
3.5的
[Quote]线程池维护尽可能少的空闲线程。对于辅助线程,此最小数目的默认值为处理器的数目。GetMinThreads 方法可获取空闲的辅助线程和 I/O 完成线程的最小数目。

在所有线程池线程都分配到任务后,线程池不会立即开始创建新的空闲线程。为避免向线程分配不必要的堆栈空间,线程池按照一定的时间间隔创建新的空闲线程。该时间间隔目前为半秒,但它在 .NET Framework 的以后版本中可能会更改。

如果应用程序遇到活动高峰,以致出现大量线程池任务排队的情况,则使用 SetMinThreads 方法可增大最小空闲线程数。否则,创建新空闲线程时的内置延迟可能导致出现瓶颈。
[/Quote]
4.0的
[Quote]线程池根据需要提供新的工作线程或 I/O 完成线程,直到其达到每个类别的最小值。 当达到最小值时,线程池可以在该类别中创建更多线程或等待某些任务完成。 从 .NET Framework 4 开始,线程池会创建和销毁工作线程以优化吞吐量,吞吐量定义为单位时间内完成的任务数。 线程过少时可能无法更好地利用可用资源,但线程过多时又可能会加剧资源的争用情况。
[/Quote]

我的破机器用的vs2008唉
zh6335901 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zzzzv0 的回复:]
引用 9 楼 xx_mm 的回复:
C# code
private static object obj_lock = new object();
static void Main(string[] args)
{
string[] ips = new string[] { "192.168.1.1", "192.168.1.2", "192.168.1.3", "19……

刚才我也……
[/Quote]
人家这是合理利用,线程操作是很耗资源的,在同一时间一个CPU只能运行一个线程的。。你自己创建500个线程然后让490多个线程在那阻塞?
zzzzv0 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xx_mm 的回复:]
C# code
private static object obj_lock = new object();
static void Main(string[] args)
{
string[] ips = new string[] { "192.168.1.1", "192.168.1.2", "192.168.1.3", "19……
[/Quote]
刚才我也试了下ThreadPool,发现了个问题:ThreadPool太抠门了!
500个空着的,就给我分了9个
zzzzv0 2012-02-24
  • 打赏
  • 举报
回复
int count = 0;
for (int i = 0; i < 255; i++)
{
//当前需要Ping的IP
string strIP = strGateWay + i.ToString();

new Thread(p =>
{
if (IsPingIP((string)p))
{
lock (arrOnline)
{
arrOnline.Add((string)p);
}
}
Interlocked.Increment(ref count);
}).Start(strIP);
}

while (count != 255)
{
Thread.Sleep(100);
}
hetengfei_ 2012-02-24
  • 打赏
  • 举报
回复

int[] sResult = new int[255];//声成全局变量
for (int i = 0; i < 255; i++)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
Thread thisThread = new Thread(pts);
string strIP = strGateWay + i.ToString();

//当前需要Ping的IP
object[] sendPara = new object[2];
sendPara[0] = strIP;
sendPara[1]= i;

thisThread.Start(strIP);

}
public void threadPing(object ip)
{
int index = (int)((object[])ip)[1];
string strIp = (string)((object[])ip)[0];;
if (IsPingIP(strIP))
{
sResult[index]=true;
}
else
{
sResult[index]=false;
}
}
蔡袅 2012-02-24
  • 打赏
  • 举报
回复
Console.WriteLine(pr.Address + ":" + pr.Status);
改为
Console.WriteLine(ips[(int)_] + ":" + pr.Status);

获取请求地址的回应。
加载更多回复(8)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧