[vtkusers] why vtkGetMacro fuction so slow

Johnny Norris jnorris at mcs.anl.gov
Sun Oct 28 15:24:38 EST 2001


Once upon a time, zhangzhijun wrote:
> I use a vtkGetMacro fuction in my program, and I find
> it's unbearable slow when the program is running,
> this is the fuction I use:vtkGetMacro(Num, short);
> and I use a for loop and do it like this
> 
> for ( i=0;i< this->GetNum();i++)
> {
>   ......................
> }
> 
> for (i=0;i<512;i++)
> {
>  .................
> }
> and it's very slow , and if I replace the this->GetNum() with the true value
> 512, it 's much more faster than before, is this all right? or what's the
> reason for this and how should I do to solve this.

In your first example, you are making a function call at the beginning of
every loop iteration.  That's why it's slower than your second example.  The
member function GetNum() is a virtual function (see the definition of
vtkGetMacro in vtkSetGet.h), so compilers won't try to inline it.  

Since this code is in a member function of the same class, you could access
Num directly:

for ( i=0;i< this->Num;i++)
{
  ......................
}

Otherwise, you should save the return value of GetNum():

int num = obj->GetNum();
for ( i=0;i< num;i++)
{
  ......................
}

Hope this helps,
John
-- 
John Norris
Research Programmer
Center for Simulation of Advanced Rockets
http://www.uiuc.edu/ph/www/jnorris



More information about the vtkusers mailing list