繁体   English   中英

C# foreach 性能低下

[英]C# foreach low performance

我正在开发一个在热敏打印机(你知道,POS 打印机等)上打印的程序这个想法是让我的 webapps 直接在这台打印机上打印而没有问题(例如斑马)无论如何,问题是对于大文档,打印需要 +/- 40 秒。 我做了一点调试,订单直接进入作业,但是,是foreach(行)慢。

距离上次用c#开发已经很久了,所以也许有人可以帮助我提高速度

try
{
    var printc = new PrintC();
    var y = 0;

    List<int> col = null;
    int tcol = 0;

    Font f;
    SizeF TestSize;
    Conversor convert = new Conversor();

    PrintDocument p = new PrintDocument();
    p.PrinterSettings.PrinterName = "RESGEST";
    p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
    {
        Graphics g = e1.Graphics;

        f = new Font("Arial", 10, FontStyle.Regular);

        dynamic obj = objeto.GetValue("el");
        foreach (dynamic item in obj)
        {
            switch ((string)item.type)
            {
                case "font":
                   // Console.WriteLine("Font: size-" + item.GetValue("size"));
                    f = new Font("Arial", (int)item.size, FontStyle.Regular);
                    break;
                case "text":
                    TestSize = g.MeasureString((string)item.texto, f);
                    TestSize = g.MeasureString((string)item.texto, f, convert.w(TestSize, (string)item.w, p, tcol));

                    int tmpy;

                    e1.Graphics.DrawString(
                        (string)item.texto, //texto
                        f, //font
                        new SolidBrush(convert.cor((string)item.cor)), //font color
                        printc.container(p, 
                        e1, 
                        convert.x((string)item.x, TestSize, p, col, tcol), //pos x
                        y, //pos y
                        convert.w(TestSize, (string)item.w, p, tcol), //width
                        tmpy = convert.h(TestSize, (string)item.h, p),  //height
                        convert.cor((string)item.background)), //background
                        printc.align((string)item.align) //align
                        );

                    if (col == null)
                    {
                        y += tmpy;
                    }
                    else
                    {
                        col.Add( tmpy);
                    }
                    break;
                case "col":
                    if ((string)item.size == "0")
                    {
                        int maxValue = col.Max();
                        y += maxValue;
                        col = null;
                        tcol = 0;
                    }
                    else
                    {
                        col = new List<int>();
                        tcol = Int32.Parse((string)item.size);
                    }
                    break;
                case "line":
                    e1.Graphics.DrawLine(new Pen(Color.Black, convert.psize((string)item.size)), 0, y += 10, (int)p.DefaultPageSettings.PrintableArea.Width, y);
                    y += 10;
                    break;
            }
        }
    };
    p.Print();


}
catch (Exception e)
{
    Console.Write("Erro " + e);
}

此代码在工作人员内部运行,该工作人员从服务器读取数据并将其发送到此功能

您需要在 Visual Studio 中分析您的代码,即使在免费版本中,它也具有强大的分析工具! 那么一个好的起点将是https://docs.microsoft.com/en-us/visualstudio/profiling/?view=vs-2017你可以从那里链接的视频开始。

然后你需要了解,如果性能缓慢是 CPU Bound 或者某些东西(例如在那个 PrintC 对象中)绑定到外部调用等待或等待数据库或实际打印机。

其他任何事情都是纯粹的猜测,因为如果没有外部引用,您的代码无法在其他任何地方进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM