本篇C#测试程序的源码如下:
//下面的演示程序的目的:
//演示如何控制HDevelop的编译
//使用集成实时编译器hdev和hdvp的过程。
//可以全局控制是否应该在执行之前编译过程。
//此外,还可以控制在什么时间点执行编译。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using HalconDotNet;
namespace UseJitCompiler
{
///
/// Summary description for Form1.
///
public class UseJitCompilerForm : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button NoCompileBtn;
internal System.Windows.Forms.Button ExplicitBtn;
internal System.Windows.Forms.Button ImplicitBtn;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
// path of HDevelop program
String ProgramPathString;
// procedure calls
private DataGridView resultGrid;
enum CompileMode
{
eNoCompile,
eExplicit,
eImplicit
};
public UseJitCompilerForm()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.NoCompileBtn = new System.Windows.Forms.Button();
this.ExplicitBtn = new System.Windows.Forms.Button();
this.ImplicitBtn = new System.Windows.Forms.Button();
this.resultGrid = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.resultGrid)).BeginInit();
this.SuspendLayout();
//
// NoCompileBtn
//
# this.NoCompileBtn.Location = new System.Drawing.Point(19, 68);
&nb{p; this.NoCompileBtn.Name = "NoCompileBtn";
this.NoCompileBtn.Size = new System.Drawing.Size(192, 55);
this.NoCompileBtn.TabIndex = 4;
this.NoCompileBtn.Text = "No compile";
this.NoCompileBtn.Click += new System.EventHandler(this.NoCompileBtn_Click);
//
// ExplicitBtn
//
this.ExplicitBtn.Location = new System.Drawing.Point(19, 145);
this.ExplicitBtn.Name = "ExplicitBtn";
this.ExplicitBtn.Size = new System.Drawing.Size(192, 56);
this.ExplicitBtn.TabIndex = 7;
this.ExplicitBtn.Text = "Explicit Compile";
this.ExplicitBtn.Click += new System.EventHandler(this.ExplicitBtn_Click);
//
// ImplicitBtn
//
this.ImplicitBtn.Location = new System.Drawing.Point(19, 223);
this.ImplicitBtn.Name = "ImplicitBtn";
this.ImplicitBtn.Size = new System.Drawing.Size(192, 55);
this.ImplicitBtn.TabIndex = 8;
this.ImplicitBtn.Text = "Implicit Compile";
this.ImplicitBtn.Click += new System.EventHandler(this.ImplicitBtn_Click);
//
// resultGrid
//
this.resultGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.resultGrid.Location = new System.Drawing.Point(217, 15);
this.resultGrid.Name = "resultGrid";
this.resultGrid.Size = new System.Drawing.Size(983, 278);
this.resultGrid.TabIndex = 9;
//
// UseJitCompilerForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(1220, 305);
this.Controls.Add(this.resultGrid);
this.Controls.Add(this.ImplicitBtn);
this.Controls.Add(this.ExplicitBtn);
this.Controls.Add(this.NoCompileBtn);
this.Name = "UseJitCompilerForm";
this.Text = "Execute procedure with and without JIT compilation";
this.Load += new System.EventHandler(this.UseJitCompilerForm_Load);
((System.ComponentModel.ISupportInitialize)(this.resultGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new UseJitCompilerForm());
}
private void UseJitCompilerForm_Load(object sender, System.EventArgs e)
{
string halconExamples = HSystem.GetSystem("example_dir");
ProgramPathString = halconExamples +
@"\hdevengine\hdevelop\test_jit_speedup.hdev";
if (!HalconAPI.isWindows)
{
ProgramPathString = ProgramPathString.Replace('\\', '/');
}
resultGrid.ColumnCount = 9;
resultGrid.RowCount = HalconAPI.isWindows ? 3 : 4;
resultGrid.RowHeadersVisible = false;
resultGrid.Columns[0].Name = "Compile Fibonacci";
resultGrid.Columns[1].Name = "Prepare Fibonacci";
resultGrid.Columns[2].Name = "Execute Fibonacci";
resultGrid.Columns[3].Name = "Compile Mean";
resultGrid.Columns[4].Name = "Prepare Mean";
resultGrid.Columns[5].Name = "Execute Mean";
resultGrid.Columns[6].Name = "Compile Process";
resultGrid.Columns[7].Name = "Prepare Process";
resultGrid.Columns[8].Name = "Execute Process";
resultGrid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
resultGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
resultGrid.AutoResizeColumns();
int height = ExplicitBtn.Top - NoCompileBtn.Top;
resultGrid.Rows[0].Height = height;
resultGrid.Rows[1].Height = height;
resultGrid.Rows[2].Height = height;
resultGrid.CurrentCell.Selected = false;
resultGrid.ReadOnly = true;
for (int i = 0; i < resultGrid.Columns.Count; i++)
{
resultGrid.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
}
}
delegate void ProcInputParamSetter(HDevProcedureCall call);
private void ShowTime(int row, int col, double seconds)
{
resultGrid[col, row].Value = String.Format("{0,7:F1} ms",1000*seconds);
resultGrid.Update();
}
private void ClearTime(int row)
{
for (int i = 0; i < resultGrid.Columns.Count; i++)
{
resultGrid[i, row].Value = "";
}
resultGrid.Update();
}
private void RunProc(string name, CompileMode mode, int rowIndex,
int columnOffset, ProcInputParamSetter setter)
{
HDevEngine Engine = null;
HDevProgram Program = null;
HDevProcedure Proc = null;
HDevProcedureCall Call = null;
try
{
Engine = new HDevEngine();
// 启用或禁用编译过程的执行,即JIT的开关
Engine.SetEngineAttribute("execute_procedures_jit_compiled",
mode == CompileMode.eNoCompile ? "false" : "true");
Program = new HDevProgram(ProgramPathString);
//载入主程序test_jit_speedup.hdev
Proc = new HDevProcedure(Program, name);
double T1 = HSystem.CountSeconds();
// 模式为预编译方式的处理
if (mode == CompileMode.eExplicit)
Proc.CompileUsedProcedures();
double T2 = HSystem.CountSeconds();
ShowTime(rowIndex, columnOffset, T2 - T1);
// 创建调用函数并设置参数
Call = new HDevProcedureCall(Proc);
setter(Call);
double T3 = HSystem.CountSeconds();
ShowTime(rowIndex, columnOffset + 1, T3 - T2);
// 执行调用函数
Call.Execute();
double T4 = HSystem.CountSeconds();
ShowTime(rowIndex, columnOffset + 2, T4 - T3);
}
catch (HDevEngineException Ex)
{
MessageBox.Show(Ex.Message, "HDevEngine Exception");
}
finally
{
// Cleanup
if (Call != null)
Call.Dispose();
if (Proc != null)
Proc.Dispose();
if (Program != null)
Program.Dispose();
if (Engine != null)
Engine.Dispose();
}
}
private void RunProcs(CompileMode mode)
{
Cursor = Cursors.WaitCursor;
int rowIndex = mode == CompileMode.eNoCompile ? 0 :
mode == CompileMode.eExplicit ? 1 : 2;
ClearTime(rowIndex);
HImage image = new HImage("fabrik");
HImage imageUInt2 = image.ConvertImageType("uint2");
RunProc("fib", mode, rowIndex, 0,
(ProcInputParamSetter) delegate(HDevProcedureCall call)
{
call.SetInputCtrlParamTuple(1,20);
});
RunProc("mean_sp_real", mode, rowIndex, 3,
(ProcInputParamSetter)delegate(HDevProcedureCall call)
{
call.SetInputIconicParamObject(1, imageUInt2);
call.SetInputCtrlParamTuple(1, 3);
call.SetInputCtrlParamTuple(2, 3);
call.SetInputCtrlParamTuple(3, 10);
call.SetInputCtrlParamTuple(4, 250);
});
RunProc("process", mode, rowIndex, 6,
(ProcInputParamSetter)delegate(HDevProcedureCall call)
{
call.SetInputIconicParamObject(1, imageUInt2);
});
imageUInt2.Dispose();
image.Dispose();
Cursor = Cursors.Default;
}
private void NoCompileBtn_Click(object sender, System.EventArgs e)
{
RunProcs(CompileMode.eNoCompile);
}
private void ExplicitBtn_Click(object sender, System.EventArgs e)
{
RunProcs(CompileMode.eExplicit);
}
private void ImplicitBtn_Click(object sender, System.EventArgs e)
{
RunProcs(CompileMode.eImplicit);
}
}
}