[Insight-developers] Performance of allocating a vector length at run-time.

Miller, James V (CRD) millerjv@crd.ge.com
Mon, 10 Dec 2001 15:07:05 -0500


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C181B6.3D2D6260
Content-Type: multipart/alternative;
	boundary="----_=_NextPart_001_01C181B6.3D2D6260"


------_=_NextPart_001_01C181B6.3D2D6260
Content-Type: text/plain;
	charset="iso-8859-1"

Here's an interesting little program to test the performance penalty of setting a vector's length at
construction time vs compile time. 
 
slowvec sets the length of a vector at constructor time.
fastvec sets the length of a vector at compile time.
 
This program runs 28 times faster using fastvec instead of slowvec!  This result is using MS VC++
6.0.  Unix machines probably have much better memory allocation schemes.

class slowvec
{
 public:
  slowvec(unsigned int dim=3);
  ~slowvec();
 
 protected:
  float *m_Data;
};
 
slowvec::slowvec(unsigned int dim)
{
  m_Data = new float[dim];
}
 
slowvec::~slowvec()
{
  if (m_Data)
    delete [] m_Data;
  m_Data = 0;
}
 
// A fast vector that avoids malloc/new
class fastvec
{
 public:
  fastvec(unsigned int dim=3);
  ~fastvec();
 
 protected:
  float m_Data[3];
};
 
fastvec::fastvec(unsigned int dim)
{
}
 
fastvec::~fastvec()
{
}
 

int main(int argc, char **argv)
{
  // fastvec* myVectors = new fastvec[100000];
  slowvec* myVectors = new slowvec[100000];
 
  delete [] myVectors;
  return 0;
}
 
 


Jim Miller 
_____________________________________
Visualization & Computer Vision
GE Corporate Research & Development
Bldg. KW, Room C218B
P.O. Box 8, Schenectady NY 12301

millerjv@crd.ge.com < mailto:millerjv@crd.ge.com <mailto:millerjv@crd.ge.com> >
(518) 387-4005, Dial Comm: 8*833-4005, 
Cell: (518) 505-7065, Fax: (518) 387-6981 


 

------_=_NextPart_001_01C181B6.3D2D6260
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=023460120-10122001><FONT size=2>Here's an interesting little 
program to test the performance penalty of setting a vector's length at 
construction time vs compile time. </FONT></SPAN></DIV>
<DIV><SPAN class=023460120-10122001><FONT size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=023460120-10122001><FONT size=2>slowvec sets the length of a 
vector at constructor time.</FONT></SPAN></DIV>
<DIV><SPAN class=023460120-10122001><FONT size=2>fastvec sets the length of a 
vector at compile time.</FONT></SPAN></DIV>
<DIV><SPAN class=023460120-10122001><FONT size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=023460120-10122001><FONT size=2>This program runs 28 times 
faster using fastvec instead of slowvec!&nbsp; This result is using MS VC++ 
6.0.&nbsp; Unix machines probably have much better memory allocation 
schemes.</FONT></SPAN></DIV>
<DIV><FONT size=2></FONT><BR><FONT size=2>class 
slowvec<BR>{<BR>&nbsp;public:<BR>&nbsp; slowvec(unsigned int dim=3);<BR>&nbsp; 
~slowvec();</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp;protected:<BR>&nbsp; float *m_Data;<BR>};</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>slowvec::slowvec(unsigned int dim)<BR>{<BR>&nbsp; m_Data = new 
float[dim];<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>slowvec::~slowvec()<BR>{<BR>&nbsp; if 
(m_Data)<BR>&nbsp;&nbsp;&nbsp; delete [] m_Data;<BR>&nbsp; m_Data = 
0;<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>// A fast vector that avoids malloc/new<BR>class 
fastvec<BR>{<BR>&nbsp;public:<BR>&nbsp; fastvec(unsigned int dim=3);<BR>&nbsp; 
~fastvec();</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp;protected:<BR>&nbsp; float m_Data[3];<BR>};</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>fastvec::fastvec(unsigned int dim)<BR>{<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>fastvec::~fastvec()<BR>{<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><BR><FONT size=2>int main(int argc, char **argv)<BR>{<BR>&nbsp; // fastvec* 
myVectors = new fastvec[100000];<BR>&nbsp; slowvec* myVectors = new 
slowvec[100000];</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp; delete [] myVectors;<BR>&nbsp; return 
0;<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV><BR>
<P><B><FONT face="Comic Sans MS" color=#000080>Jim Miller</FONT></B> 
<BR><B><I><FONT face=Arial color=#ff0000 
size=2>_____________________________________</FONT></I></B><I></I><BR><I></I><I><FONT 
face=Arial color=#000000 size=1>Visualization &amp; Computer Vision<BR>GE 
Corporate Research &amp; Development<BR>Bldg. KW, Room C218B<BR>P.O. Box 8, 
Schenectady NY 12301<BR><BR></FONT><U><FONT face=Arial color=#0000ff 
size=1>millerjv@crd.ge.com &lt;<A 
href="mailto:millerjv@crd.ge.com">mailto:millerjv@crd.ge.com</A>&gt;</FONT></U></I><BR><I><FONT 
face=Arial color=#000000 size=1>(518) 387-4005, Dial Comm: 8*833-4005, 
</FONT></I><BR><I><FONT face=Arial color=#000000 size=1>Cell: (518) 505-7065, 
Fax: (518) 387-6981</FONT></I> </P><BR>
<DIV>&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01C181B6.3D2D6260--

------_=_NextPart_000_01C181B6.3D2D6260
Content-Type: application/octet-stream;
	name="Miller, James V (CRD).vcf"
Content-Disposition: attachment;
	filename="Miller, James V (CRD).vcf"

BEGIN:VCARD
VERSION:2.1
N:Miller;James
FN:Miller, James V (CRD)
ORG:CRD;ESL
TITLE:Computer Scientist
TEL;WORK;VOICE:*833-4005
TEL;WORK;VOICE:1 518 387-4005
ADR;WORK:;KW-C218B;P.O. Box 8;Schenectady;New York;12301;USA
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:KW-C218B=0D=0AP.O. Box 8=0D=0ASchenectady, New York 12301=0D=0AUSA
EMAIL;PREF;INTERNET:millerjv@crd.ge.com
REV:20010420T140329Z
END:VCARD

------_=_NextPart_000_01C181B6.3D2D6260--