跟踪实例

1.1 初步跟踪的实现

    我们想要让trace 对象记录一些事件消息,比如进入某个函数,离开某个函数,以及其他介入这两者之间的某些重要消息。如下:
 C++ Code 
1
2
3
4
5
6
7
8
int myFunction( int x)
{
    string name =  "myFunction";
    Trace t(name);
    ...
    string moreInfo =  "more interesting info";
    t.debug(moreInfo);
}

我们下面的Trace类来实现这一功能。
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Trace
{
public:
    Trace( const string &name);
    ~Trace();
     void debug( const string &name);

     static  bool traceIsActive;
private:
    string theFunctionName;
};

inline Trace::Trace( const string &name): theFunctionName(name)
{
     if(traceIsActive)
    {
        cout <<  "Enter function" << name << endl;
    }
}

inline  void Trace::debug( const string &msg)
{
     if(traceIsActive)
    {
        cout << msg << endl;
    }
}

inline Trace::~Trace()
{
     if(traceIsActive)
    {
        cout <<  "Exit function" << theFunctionName << endl;
    }
}
    这种实现方法,在关键执行路径的大多函数中突然出现Trace对象。在紧接着性能测试中,我们震惊地发现性能将为原来性能的20%。原因在于:
   1、I/O的开销是高昂的。
   2、函数调用的开销是要考虑的一个因素,因此我们应该讲短小、频繁调用的函数内联。
   3、复制对象的开销是高昂的,最好选择引用,而不是传值。

    我们的做法是尽量少定义string类型。减少对象的定义和销毁的代价。利用const char *代替string来实现,下面的是完整代码。
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
#include<string>
  #include<windows.h>

using  namespace std;
/*
class Trace
{
public:
    Trace(const string &name);
    ~Trace();
    void debug(const string &name);

    static bool traceIsActive;
private:
    string theFunctionName;
};

inline Trace::Trace(const string &name):theFunctionName(name)
{
 if(traceIsActive)
 {
   cout<<"Enter function"<<name<<endl;
 }
}

inline void Trace::debug(const string &msg)
{
 if(traceIsActive)
 {
   cout <<msg<<endl;
 }
}

inline Trace::~Trace()
{
 if(traceIsActive)
 {
  cout<<"Exit function"<<theFunctionName<<endl;
 }
}*/

//改进版
class Trace
{
public:
    Trace( const  char *name);
    ~Trace();
     void debug( const string &name);

     static  bool traceIsActive;
private:
    string *theFunctionName;
};

inline Trace::Trace( const  char *name):theFunctionName( 0)
{
  if(traceIsActive)
 {
   cout<< "Enter function"<<name<<endl;
   theFunctionName= new string(name);
 }
}

inline  void Trace::debug( const string &msg)
{
  if(traceIsActive)
 {
   cout <<msg<<endl;
 }
}

inline Trace::~Trace()
{
  if(traceIsActive)
 {
  cout<< "Exit function"<<*theFunctionName<<endl;
   delete theFunctionName;
 }
}

bool Trace::traceIsActive= true;

int addOne( int x)
{
  char *name= "addOne";
 Trace t(name);
  return x+ 1;
}
int main()
{
  SYSTEMTIME t1,t2;  
   int j= 10000000;
   int y= 0;
  Trace::traceIsActive= false;

  GetSystemTime(&t1); 
   for( int i= 0;i<j;i++)
  {
   y=addOne(i);
  }
  GetSystemTime(&t2); 
  cout<<t2.wMilliseconds-t1.wMilliseconds<<endl;
  system( "pause");
}


1.2 要点

    1、对象定义触发隐形地执行构造函数和析构函数。我们称其为“隐形执行”而不是“隐形开销”,是因为对象的构造和销毁并不总是意味着产生开销。内联函数使用减少函数调用的开销。
   2、通过引用传递对象还是不能保证良好的性能,所以避免对象的复制的确有利于提高性能。
   3、不要把精力浪费在计算结果根本不会被使用的地方。
   4、在完成同样的简单工作,char指针有时比string对象更加有效。
   5、内联消除了常被使用的小函数调用所产生的函数开销。









































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mht(Multiple Hypothesis Tracking)多假设跟踪算法是一种用于目标跟踪的算法。它的基本思想是在每一帧图像中生成多个可能的目标轨迹假设,并通过匹配当前帧的观测数据来更新和筛选这些假设,以实现准确的目标跟踪。 下面以一个实例来说明mht多假设跟踪算法的应用过程: 在一段监控视频中,我们需要跟踪一个行人的运动轨迹。首先,通过图像处理和目标检测算法,我们从视频帧中提取行人的位置信息作为初始观测数据。 接下来,使用mht算法生成初始的跟踪假设。假设我们生成了两个初始轨迹假设A和B,分别代表行人可能的运动轨迹。假设A猜测行人会向左移动,假设B猜测行人会向右移动。 然后,对于每个新的视频帧,我们在每个假设轨迹上预测下一时刻的行人位置,并与当前帧的观测数据进行比较。假设A在预测结果与观测数据的匹配度更高,说明行人更有可能向左移动,而假设B的匹配度较低,说明行人更不可能向右移动。 接着,我们使用匹配度的得分来更新假设轨迹的权重,并进行筛选。匹配度高的假设轨迹会得到更高的权重,而匹配度低的假设轨迹会被剔除。通过不断迭代更新和筛选,我们可以得到一个更准确的行人运动轨迹。 最后,根据最终轨迹假设的权重,我们可以确定行人的运动路径和速度,并进行进一步的分析和预测。 总之,mht多假设跟踪算法通过生成多个可能的目标轨迹假设,并利用观测数据的匹配度来更新和筛选这些假设,实现准确的目标跟踪。在实际应用中,mht算法可以用于视频监控、自动驾驶、目标识别等领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值