【发布时间】:2014-02-27 03:00:42
【问题描述】:
我正在使用 Visual Studio 2010 开发应用程序,并且正在使用 C++/CLI 进行编码。
在我之前的问题中,我遇到了来自串行端口的数据系列问题。现在看起来没问题,现在我正在尝试绘制它们。
我不想使用Chart,所以我想使用使用Graphics 类的手工函数。为了测试我的代码,我创建了两个数组:第一个数组填充了来自高斯的值。第二个是随机数。
当我绘制这些值时,我希望看到我的图像示波器一样增长和更新。第二个Do_Plot 设法通过BackColor 绘制它们的“删除点”。
所有代码都按预期工作,但我遇到了性能问题。如果我在我的电脑上运行代码,我的系列每 500/700 毫秒绘制一次。
有时它会减慢到 1500 毫秒,然后恢复得更快。
我试图在我同事的电脑上运行代码,我注意到第一个系列每 170 毫秒绘制一次,而第二个系列每 950 毫秒绘制一次。
这是代码:
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
button1->Enabled = false;
array<float,1>^ Gauss = gcnew array<float,1>(1001);
array<float,1>^ Rumore = gcnew array<float,1>(1001);
/*Some useful variables*/
Random^ generatore = gcnew Random;
float a = safe_cast<float>(Math::Round(5/(SIGMA*Math::Sqrt(2*PI)), 2));
float b = safe_cast<float>(2*(SIGMA*SIGMA));
/*Start */
float portante;
float r;
float s;
int convX =1000/1000;
int convY =500/2;
/*time variables */
int bias = 50;
int dif =600;
/*Gap between drawing and removing*/
int k = 3;
int e1 = 0;
for ( ; ; ) {
/*Start*/
clock_t Start = clock();
if(textBox1->Text==""){
portn = 5;
}
else
portn = float::Parse(textBox1->Text);
/*temp variables to go out the for cycle */
portante = portn;
r = rand;
s = sig;
/ckeck state is OK */
check = 0;
for(int i = 1; i<=1000; i++) {
Gauss[i] = safe_cast<float>(Math::Round( a*s*Math::Exp(-Math::Pow(((0.01*1*(i))-portante), 2)/b), 2));
Rumore[i] = safe_cast<float>(Math::Round(r*generatore->NextDouble(), 2));
bool clipSup = ClipSup(2, Gauss[i]+Rumore[i]);
if(clipSup==true) {
Gauss[i] = 1.99f;
Rumore[i] = 0;
}
Do_Plot(g, disegna, i-1, Gauss[i-1]+Rumore[i-1], i, Gauss[i]+Rumore[i], convX, convY);
e1 =(k+i)%1000;
Do_Plot(g, rimuovi, e1, Gauss[e1]+Rumore[e1], e1+1, Gauss[e1+1]+Rumore[e1+1], convX, convY);
/*Ckeck if go out for cycle*/
if(check == CODE_1 ) {
portante = portn;
break;
}
if(check == CODE_2 ) {
r = rand;
break;
}
if(check == CODE_3 ) {
s = sig;
break;
}
}
clock_t Stop = clock();
int Diff = Stop-Start;
label8->Text = Convert::ToString(Diff);
int tempDiff = (Stop-Start)+bias;
if(tempDiff>dif)
{
//Do_Axes(g); /*Do Axes*/
//Do_Grid(g); /*Do Grid */
Application::DoEvents();
dif = 600;
bias = 0;
}
else
bias +=50; //Else bias grows
}
}
Do_Plot 在哪里:
void Do_Plot(Graphics^ g, Pen^ penna, int Xi, float Yi, int Xf, float Yf, int convX, int convY) {
g->DrawLine(penna, (convX*Xi+50), safe_cast<int>(500-(Yi*convY)+50),
(convX*Xf+50), safe_cast<int>(500-(Yf*convY)+50));
}
我在这里声明了Graphics^ g:
public ref class Form1 : public System::Windows::Forms::Form
{
Graphics^ g;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: aggiungere qui il codice del costruttore.
//
g = pictureBox1->CreateGraphics();
}
我不知道为什么我的代码在另一台 PC 上运行时会如此不同。我认为问题出在g = pictureBox1->CreateGraphics();,但我只是在做一些假设。任何形式的帮助都将不胜感激,因为自上周以来我一直坚持这一点!
非常感谢!
埃米利亚诺
【问题讨论】:
-
您确定是
Do_Plot()减慢了进程,而不是循环中涉及的数学吗?如果您想要真正的快速绘图,请使用普通 GDI 而不是 GDI+。 -
感谢您的回复!我不认为数学是问题。如果不调用该函数,则每个循环持续约 1 毫秒。
标签: winforms performance c++-cli picturebox