C# 为什么并行多线程代码执行比顺序代码执行慢?

C# 为什么并行多线程代码执行比顺序代码执行慢?,c#,multithreading,.net-core,parallel-processing,task-parallel-library,C#,Multithreading,.net Core,Parallel Processing,Task Parallel Library,我想使用多线程的矩形和梯形方法执行积分计算,以获得更快的结果。 不幸的是,在我的例子中,执行多线程代码比标准顺序代码慢。 使用多个线程比使用单个线程慢得多——毕竟,不是应该反过来吗? 感觉线程越多,代码执行越慢 此外,我注意到线程越多,积分结果的精度就越低。这在使用梯形方法计算积分时尤其明显 这是我的密码: 使用系统; 使用系统诊断; 使用系统线程; 使用System.Threading.Tasks; 命名空间ParallelProgramming.ConsoleApp { 班级计划 { 公共

我想使用多线程的矩形和梯形方法执行积分计算,以获得更快的结果。 不幸的是,在我的例子中,执行多线程代码比标准顺序代码慢。 使用多个线程比使用单个线程慢得多——毕竟,不是应该反过来吗? 感觉线程越多,代码执行越慢

此外,我注意到线程越多,积分结果的精度就越低。这在使用梯形方法计算积分时尤其明显

这是我的密码:

使用系统;
使用系统诊断;
使用系统线程;
使用System.Threading.Tasks;
命名空间ParallelProgramming.ConsoleApp
{
班级计划
{
公共静态字符串集成方法{get;set;}
公共静态双间隔开始{get;set;}
公共静态双IntervalEnd{get;set;}
公共静态int NPrecisionValue{get;set;}
公共静态bool是并行的{get;set;}
公共静态int线程值{get;set;}
公共静态秒表秒表{get;set;}
公共静态双结果{get;set;}
静态void Main(字符串[]参数)
{
Console.WriteLine(“函数|运行时间|估计积分”);
Console.WriteLine(“------------------------------------------------------------------”;
间隔开始=5;
IntervalEnd=-2;
n精度值=100000000;
//直肠角融合-序贯
数值积分方法积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分矩形积分(间隔开始、间隔结束、非精确值);
秒表;
Console.WriteLine($“{nameof(integral.RectangularIntegration)}–Sequential{Stopwatch.appeased}{Result}”);
//直角平行-1螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。矩形积分平行(间隔开始,间隔结束,非精确值,1);
秒表;
Console.WriteLine($“{nameof(integral.RectangularIntegrationParallel)}-1线程{Stopwatch.appeased}{Result}”);
//直角平行-2个螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。矩形积分平行(间隔开始,间隔结束,非精确值,2);
秒表;
Console.WriteLine($“{nameof(integral.RectangularIntegrationParallel)}–2个线程{Stopwatch.appeased}{Result}”);
//矩形平行-3螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。矩形积分平行(间隔开始,间隔结束,非精确值,3);
秒表;
WriteLine($“{nameof(integral.RectangularIntegrationParallel)}–3个线程{Stopwatch.appeased}{Result}”);
//矩形平行-4螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。矩形积分平行(间隔开始,间隔结束,非精确值,4);
秒表;
Console.WriteLine($“{nameof(integral.RectangularIntegrationParallel)}–4个线程{Stopwatch.appeased}{Result}”);
//梯形积分-序贯
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。梯形积分(区间开始、区间结束、N精度值);
秒表;
Console.WriteLine($“{nameof(integral.梯形积分)}–Sequential{Stopwatch.appeased}{Result}”);
//梯形积分平行–1螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。梯形积分平行(间隔开始,间隔结束,n精度值,1);
秒表;
Console.WriteLine($“{nameof(integral.梯形积分并行)}–1线程{Stopwatch.appeased}{Result}”);
//梯形积分平行–2个螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。梯形积分平行(间隔开始,间隔结束,n精度值,2);
秒表;
Console.WriteLine($“{nameof(integral.梯形积分并行)}–2个线程{Stopwatch.appeased}{Result}”);
//梯形积分平行–3个螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。梯形积分平行(间隔开始,间隔结束,n精度值,3);
秒表;
Console.WriteLine($“{nameof(integral.梯形积分并行)}–3个线程{Stopwatch.appeased}{Result}”);
//梯形积分平行–4个螺纹
积分=新();
Stopwatch=Stopwatch.StartNew();
结果=积分。梯形积分平行(间隔开始,间隔结束,n精度值,4);
秒表;
Console.WriteLine($“{nameof(integral.梯形积分并行)}–4个线程{Stopwatch.appeased}{Result}”);
Console.WriteLine(“按任意键继续…”);
Console.ReadLine();
}
}
公共类数值积分方法
{
双功能(双x)
{
返回x*x+2*x;
}
公共双矩形
Function                                  | Elapsed Time     | Estimated Integral
-----------------------------------------------------------------
RectangularIntegration – Sequential | 00:00:00.9284260 | -65.33333210831276
RectangularIntegrationParallel – 1 Thread | 00:00:01.7040507 | -65.33333210831276
RectangularIntegrationParallel – 2 Threads | 00:00:01.7191484 | -65.33333210831276
RectangularIntegrationParallel – 3 Threads | 00:00:01.6888398 | -57.73164823448317
RectangularIntegrationParallel – 4 Threads | 00:00:01.5530828 | -65.33333210831276
TrapezoidalIntegration – Sequential | 00:00:00.7278303 | -65.33333333332568
TrapezoidalIntegrationParallel – 1 Thread | 00:00:01.4265208 | -65.33333333332568
TrapezoidalIntegrationParallel – 2 Threads | 00:00:02.3009881 | -33.110522448239216
TrapezoidalIntegrationParallel – 3 Threads | 00:00:01.6062253 | -57.02137898750542
TrapezoidalIntegrationParallel – 4 Threads | 00:00:01.9967140 | -18.120285251376426
public double RectangularIntegrationParallel(double xp, double xk, int n, int maxThreads)
{
    double dx, integral = 0;
    dx = (xk - xp) / n;
    var locker = new object();

    Parallel.ForEach(Partitioner.Create(0, n + 1), new ParallelOptions
    {
        MaxDegreeOfParallelism = maxThreads
    }, () => 0.0D, (range, state, accumulator) =>
    {
        for (int i = range.Item1; i < range.Item2; i++)
        {
            accumulator += dx * Function(xp + i * dx);
        }
        return accumulator;
    }, accumulator =>
    {
        lock (locker) { integral += accumulator; }
    });
    return integral;
}
public static ParallelLoopResult ForEach<TSource, TLocal>(
    Partitioner<TSource> source,
    ParallelOptions parallelOptions,
    Func<TLocal> localInit,
    Func<TSource, ParallelLoopState, TLocal, TLocal> body,
    Action<TLocal> localFinally);
public double RectangularIntegrationParallel(double xp, double xk, int n, int maxThreads)
{
    double dx = (xk - xp) / n;

    return Partitioner.Create(0, n + 1)
        .AsParallel()
        .WithDegreeOfParallelism(maxThreads)
        .Select(range =>
        {
            double integral = 0.0;
            for (int i = range.Item1; i < range.Item2; i++)
            {
                integral += dx * Function(xp + i * dx);
            }
            return integral;
        })
        .Sum();
}