C#中的并行计算 [英] Parllel Computing in c#

查看:94
本文介绍了C#中的并行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个顺序循环,如...

i have a sequential loop as...

for (int i = 0; i < 1000;i++ )
            {
                Console.WriteLine(i);
            }


而且此循环的执行时间约为t微秒.现在,我想使其与我的4核i5处理器并行.这样我可以减少执行时间..所以我写了这段代码...



and execution time for this loop is some t microsecond.now i want to make it parllel with my i5 processor of 4 cores. so that i can reduce the execution time for it.. so i had written this code...


int N=1000;
Parallel.For(0, N, delegate(int i) 
                {
                    Console.WriteLine(i);
                });


但这花费了比t更长的执行时间,请帮助我..::<


but this took execution time more than t ,, pls help me in this.. :(

推荐答案

我自己没有使用过Parallel,但是我建议Console.WriteLine(i);语句会使速度变慢,因为两者都在争用共享资源.
I have not used Parallel myself but I would suggest that the Console.WriteLine(i); statements will slow things down, as both will be contending for shared resources.


这篇关于C#中的并行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆