c# - 并行 C# 线程性能问题

标签 c# multithreading performance parallel-processing

这段代码用于从彩色到黑白的转换。

  • 我尝试在代码中添加并行部分
  • 只是从灰度转换为黑白
  • 我尝试为每个线程分配一些具有像素数量的 cols

但是性能根本没有提高。

我尝试通过更改 2 个 for 循环中的除数值(代码中为 800)和计数器值增量来获得 800*600 图像。

这个改变应该会使线程数增加或减少

结果:

value |  num of thread |   time
 800       1              1.17 sec
 400       2              1.17 sec
 200       4              1.17 sec
and so on ...

帮助我提高性能,因为更大的图像需要 8 秒,我想让它并行

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace IMG
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    string path = "";
    public void openimage()
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            path = openFileDialog1.FileName;
        }
    }
    Bitmap bm;
    Bitmap gs;
    private void button1_Click(object sender, EventArgs e)
    {
        if (path == "")
        {
            openimage();
        }
        Graphics g = this.CreateGraphics();
        bm = new Bitmap(path);
        // Draw image with no effects.....
        g.DrawImage(bm, 100, 100, 200, 200);
        gs = new Bitmap(bm.Width, bm.Height);
        worker soso = new worker(gs, bm);
        worker.counter = 0;
        soso.tograyscale();
        // Draw gray image.......
        g.DrawImage(gs, 305, 100, 200, 200);
        DateTime x = DateTime.Now;
        for (int koko = 0; koko < gs.Width/400; koko++)
        {        
                Thread t1 = new Thread(new ThreadStart(soso.trasform));
                t1.Start();
                t1.Join();
                soso.increment();
        }
        g.DrawImage(bm, 510, 100, 200, 200);
        gs.Dispose();
        g.Dispose();
        textBox19.Text=(DateTime.Now - x).ToString();
    }
}
public class worker
{
    public static int counter = 0;
    public Bitmap gs;
    public Bitmap bm;
    public int xxxx;
    public worker(Bitmap a, Bitmap b)
    {
        gs = a;
        bm = b;
        xxxx = a.Height;
    }
    public void increment()
    {
        counter += 400;
    }
    public void trasform()
    {
        argtransform(counter);

    }
    public void tograyscale()
    {
        for (int i = 0; i < bm.Width; i++)
        {
            for (int j = 0; j < bm.Height; j++)
            {
                Color c = bm.GetPixel(i, j);
                int y = (int)(0.3 * c.R + 0.59 * c.G + 0.11 * c.B);
                gs.SetPixel(i, j, Color.FromArgb(y, y, y));
            }
        }
    }
    public void argtransform(int cc)
    {
        for (int i = cc; i < cc + 400; i++)
        {
            for (int j = 0; j < xxxx; j++)
            {
                Color c = gs.GetPixel(i, j);
                int y1 = 0;
                if (c.R >= 128)
                    y1 = 255;
                bm.SetPixel(i, j, Color.FromArgb(y1, y1, y1));
            }
        }
    }
  }
}

谢谢你尽快


谢谢大家 Craig Stuntz 的问题:如何在不调用 setpixel 的情况下编辑像素的颜色? 谢谢你的帮助

最佳答案

在优化之前不要并行化。在一个好的分析器下运行你的代码并首先修复性能问题。是的,这最终可能会成为并行化的一个很好的候选者,但我敢打赌,对 SetPixel 的 100 万次调用是一个更大的问题。

关于c# - 并行 C# 线程性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2042952/

相关文章:

c# - 在对象的实例上为同一方法创建多个线程

c# - 在 .net 中同步特定线程操作

java - CountdownTimer 崩溃应用程序。尝试更新 UI 线程

java - 线程化可运行对象方法不响应更改

ruby-on-rails - 是否可以在 Ruby 1.9 中透明地实现 ActiveRecord 查询的 Future 模式?

c# - 验证的最佳位置在哪里......构造函数或留给客户端调用?

c# - 无法加载文件或程序集 'EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

performance - 为什么选择不同的分区列很慢?

python - 在总和有限的情况下以最快的速度优化面积差

C# - Windows 窗体应用程序 - 保存文件