page432.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 122 行

HTML
122
字号
<HTML><HEAD><TITLE>Projects</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6143" HREF="page433.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6141" HREF="page415.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6137" HREF="page431.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6145" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0013700000000000000000">Projects</A></H1><P><OL><LI>	Devise and conduct a set of experiments	to measure garbage collection overhead.	For example, write a program that	creates a specified number of garbage objects as quickly as possible.	Determine the number of objects needed to trigger garbage collection.	Measure the running time of your program when no garbage collection	is performed and compare it to the running time observed	when garbage collection is invoked.<LI>	Python does not provide the means for accessing memory directly.	Consequently, it is not possible	to implement the Python heap in Python (without	using native methods).	Nevertheless, we can <em>simulate</em>	a heap using a Python array of <tt>int</tt>s.	Write a Python class that manages an array of <tt>int</tt>s.	Your class should implement the following methods:<PRE>class Heap(object):    def acquire(self, size):	&quot;(Heap, int) -&gt; int&quot;	# ...    def release(self, offset):	&quot;(Heap, int) -&gt; None&quot;	# ...    def __getitem__(self, offset):	&quot;(Heap, int) -&gt; int&quot;	# ...    def __setitem__(self, offset, value):	&quot;(Heap, int, int) -&gt; None&quot;	# ...</PRE>	The <tt>acquire</tt> method allocates a region of <tt>size</tt>	consecutive <tt>int</tt>s in the array and returns the offset of the	first <tt>int</tt> in the region.	The <tt>release</tt> method release a region of <tt>int</tt>s	at the specified offset which was	obtained previously using <tt>acquire</tt>.	The <tt>__getitem__</tt> and <tt>__setitem__</tt> methods	access a value in the array at a given offset.<LI><A NAME="projectgarbageproject3">&#160;</A>	Using an array of <tt>int</tt>s simulate the <em>mark-and-sweep</em>	garbage collection as follows:	<OL><LI>		Write a <tt>Handle</tt> class		that contains the methods given below:<PRE>class Handle(Object):    def getSize(self):	&quot;(Handle) -&gt; int&quot;	# ...    getReference(self, offset):	&quot;(Handle, int) -&gt; Handle&quot;	# ...    setReference(self, offset, handle):	&quot;(Handle, int, Handle) -&gt; None&quot;	# ...    def __getitem__(self, offset):	&quot;(Handle, int) -&gt; int&quot;	# ...    def __setitem__(self, offset, value):	&quot;(Handle, int, int) -&gt; int&quot;	# ...</PRE>		A handle refers to an object that contains		either <tt>int</tt>s or other handles.		The size of an object is total the number of <tt>int</tt>s		and handles it contains.		The various store and fetch methods are used to		insert and remove items from the object to which this		handle refers.<LI>		Write a <tt>Heap</tt> class that contains the methods		given below:<PRE>class Heap(object):    def acquire(self, size):	&quot;(Heap, int) -&gt; Handle&quot;	# ...    def release (self, handle):	&quot;(Heap, Handle) -&gt; None&quot;	# ...    def collectGarbage(self):	&quot;(Heap) -&gt; None&quot;	#</PRE>		The <tt>acquire</tt> method allocates a handle and		space in the heap for an object of the given size.		The <tt>release</tt> method releases the given handle		but does not reclaim the associated heap space.		The <tt>collectGarbage</tt> method performs the		actual garbage collection operation.	</OL><LI>	Using the approach described in Project&nbsp;<A HREF="page432.html#projectgarbageproject3"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,	implement a simulation of <em>mark-and-compact</em> garbage collection.<LI>	Using the approach described in Project&nbsp;<A HREF="page432.html#projectgarbageproject3"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>	implement a simulation of <em>reference-counting</em> garbage collection.</OL><P><HR><A NAME="tex2html6143" HREF="page433.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6141" HREF="page415.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6137" HREF="page431.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6145" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?