浅谈Halcon的执行时间和算子时间

本文深入探讨了Halcon中执行时间和算子时间的区别,并通过实验验证了这两种时间测量的实际意义,尤其是在不同环境下运行Halcon代码时的表现差异。

浅谈Halcon的执行时间和算子时间

0. 引子——算法耗时需求

使用Halcon的小伙伴总会遇上这样的需求:需要知道不同的算子在图像处理算法中耗费了多少时间. Halcon的Hdevelop GUI上,可以通过计时工具显示算子执行时的时间(以绝对时间计或以百分比计).

但是初学Halcon的同学可能会有一些疑问:为什么这个算子的时间还分为“执行时间”和“算子时间”,这二者到底哪一个才是真正的耗时,它们二者之间又到底有什么区别?

在这里插入图片描述

1. Halcon的执行时间和算子时间

遇事不决,先查文档.

Halcon虽然没有太多的中文资料能查,但是它软件自带的帮助文档已经相当详尽.

但是Halcon的帮助文档是几乎全英文的,所以同学们要好好学习英语.

执行时间 - execution time

算子时间 - operator time

在6.4.10.1 Profiler Display中可以查到:

在这里插入图片描述

在6.4.10 Profiler中可以查到:

在这里插入图片描述

The built-in profiler analyzes the runtime behavior of HDevelop programs. It counts operator and procedure calls, and measures the processing times of operator calls (referred to as operator time in the following). The operator time plus the additional overhead of each operator call inside HDevelop is measured as well (referred to as execution time in the following). The operator time is the appropriate measure if you plan to export your HDevelop program to a programming language. On the other hand, the execution time is the appropriate measure if you run the program inside HDevelop or HDevEngine.

从这段中我们可以得到信息:

执行时间 相当于 算子时间+算子调用的加载时间.

The operator time is the appropriate measure if you plan to export your HDevelop program to a programming language.

这句话的意思是,如果你想要将halcon代码导出成其他编程语言的代码,算子时间可以作为它的参考.

2. Halcon算子执行时间的测试

从文档里面我们已经得到了初步的结论,我们先在hDevelop中验证一下:

把Halcon的例程之一:threshold.hdev改造成以下代码

read_image (Audi2, 'audi2')
fill_interlace (Audi2, ImageFilled, 'odd')
for i:= 1 to 100 by 1
    threshold (ImageFilled, Region, 0, 90)
endfor
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'width', 'and', 30, 70)
select_shape (SelectedRegions, Letters, 'height', 'and', 60, 110)
dev_clear_window ()
dev_set_colored (12)
dev_display (ImageFilled)
dev_display (Letters)

注意我们是把threshold(ImageFilled, Region, 0, 90)这一句连续运行100次,这并不会改变整个程序的运行结果,但有助于我们把这个算子运行100次的平均时间测算出来.

在这里插入图片描述

可以看到,在以上的测试方式下,测算出的执行时间约为3.561ms.

在这里插入图片描述

测算出的算子时间为0.203ms.

很明显,执行时间远远大于算子时间,这也符合我们从文档中查到的第一条信息:执行时间是算子时间和算子调用的加载时间之和.

接下来我们来看看把程序导出成C++代码后,测算的时间.

///////////////////////////////////////////////////////////////////////////////
// File generated by HDevelop for HALCON/C++ Version 18.11.0.1
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// Ensure that the interface encoding is set to locale encoding by calling
// SetHcppInterfaceStringEncodingIsUtf8(false) at the beginning of the program.
// 
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
// 
// Source files with different encoding should not be mixed in one project.
///////////////////////////////////////////////////////////////////////////////

#include <Windows.h>
#include <iostream>

#ifndef __APPLE__
#  include "HalconCpp.h"
#  include "HDevThread.h"
#  if defined(__linux__) && (defined(__i386__) || defined(__x86_64__)) \
                         && !defined(NO_EXPORT_APP_MAIN)
#    include <X11/Xlib.h>
#  endif
#else
#  ifndef HC_LARGE_IMAGES
#    include <HALCONCpp/HalconCpp.h>
#    include <HALCONCpp/HDevThread.h>
#  else
#    include <HALCONCppxl/HalconCpp.h>
#    include <HALCONCppxl/HDevThread.h>
#  endif
#  include <stdio.h>
#  include <HALCON/HpThread.h>
#  include <CoreFoundation/CFRunLoop.h>
#endif



using namespace HalconCpp;

// Procedure declarations 
// Chapter: Develop
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
	HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle);

// Procedures 
// Chapter: Develop
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
	HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle)
{
   
   

	// Local iconic variables

	// Local control variables
	HTuple  hv_MinWidth, hv_MaxWidth, hv_MinHeight;
	HTuple  hv_MaxHeight, hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
	HTuple  hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;

	//This procedure opens a new graphics window and adjusts the size
	//such that it fits into the limits specified by WidthLimit
	//and HeightLimit, but also maintains the correct image aspect ratio.
	//
	//If it is impossible to match the minimum and maximum extent requirements
	//at the same time (f.e. if the image is very long but narrow),
	//the maximum value gets a higher priority,
	//
	//Parse input tuple WidthLimit
	if (0 != (HTuple((hv_WidthLimit.TupleLength()) == 0).TupleOr(hv_WidthLimit < 0)))
	{
   
   
		hv_MinWidth = 500;
		hv_MaxWidth = 800;
	}
	else if (0 != ((hv_WidthLimit.TupleLength()) == 1))
	{
   
   
		hv_MinWidth = 0;
		hv_MaxWidth = hv_WidthLimit;
	}
	else
	{
   
   
		hv_MinWidth 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值