C# foreach 低性能
C# foreach low performance
我正在开发一个在热敏打印机(您知道的 POS 打印机等)上打印的程序
这个想法是让我的网络应用程序直接在这台打印机上打印而不会出现问题(例如 zebra)
无论如何,问题是对于大文档,打印需要 +/- 40 秒。
稍微debug了一下,job中命令直接进入了,但是慢的是foreach(line)。
距离上次用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 绑定或者某些东西(例如在那个 PrintC 对象中)绑定到等待或等待数据库或实际打印机的外部调用。
其他任何事情都是纯粹的猜测,因为如果没有外部引用,您的代码无法在其他任何地方进行测试。
我正在开发一个在热敏打印机(您知道的 POS 打印机等)上打印的程序 这个想法是让我的网络应用程序直接在这台打印机上打印而不会出现问题(例如 zebra) 无论如何,问题是对于大文档,打印需要 +/- 40 秒。 稍微debug了一下,job中命令直接进入了,但是慢的是foreach(line)。
距离上次用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 绑定或者某些东西(例如在那个 PrintC 对象中)绑定到等待或等待数据库或实际打印机的外部调用。
其他任何事情都是纯粹的猜测,因为如果没有外部引用,您的代码无法在其他任何地方进行测试。