(转)Transaction与OpenCloseTransaction效率分析
(2010-12-08 22:04:11)
www.objectarx.net 忽悠悠原创,转载请注明.
前些天在群里讨论到Transaction读写实体的效率问题,今天就做了一段代码说明一下
Transaction的效率.Transaction是什么东西这里就不做讨论了,有兴趣的朋友可以在论坛里面搜索以前的帖子.
Transaction有一个子类,叫做OpenCloseTransation,在文档中这样描述它
This class may be used instead of the transaction class in certain
scenarios..It wraps the ObjectId.Open/Close functions, but makes it
easier to correctly pair these functions by storing references to
every object opened and automatically closing them.
也就是说在对实体进行打开关闭操作的时候,它可以替代Transaction类.
我们通过下面代码来测试OpenCloseTransation和Transaction的效率上的区别.
[CommandMethod("GetTransTime")]
public void
GetTransTime()
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
Stopwatch
watch = new Stopwatch();
PromptSelectionResult res =
ed.SelectAll();
if
(res.Status != PromptStatus.OK)
{
return;
}
ed.WriteMessage("\nCount of entities : {0}",
res.Value.Count.ToString());
watch.Start();
using
(Transaction trans =
HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
foreach
(ObjectId id in res.Value.GetObjectIds())
{
Entity ent =
trans.GetObject(id, OpenMode.ForWrite) as Entity;
ent.ColorIndex
= 1;
}
trans.Commit();
}
watch.Stop();
ed.WriteMessage("\nOne Transaction write cost:
{0}(ms)", watch.ElapsedMilliseconds.ToString());
watch.Reset();
watch.Start();
using
(Transaction trans =
HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
foreach
(ObjectId id in res.Value.GetObjectIds()) |