<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Tuning|Main Operations Performance|Query Performance" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Complexity Of Objects </title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../../Resources/Code/prettify.js"> </script> <script src="../../../Resources/Code/lang-vb.js"> </script> <script src="../../../Resources/Code/jquery.min.js"> </script> <script src="../../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../index_CSH.html#tuning/main_operations_performance/query_performance/complexity_of_objects.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../tuning.htm">Tuning</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../main_operations_performance.htm">Main Operations Performance</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../query_performance.htm">Query Performance</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Complexity Of Objects</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>Complexity Of Objects</h1> <p>More complex objects are usually more difficult not only to store, but also to query and instantiate. The following test demonstrates how query performance depends on class structure, complexity and depth:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET"> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: RunDifferentObjectsTest private void RunDifferentObjectsTest() { Init(); System.Console.WriteLine("Storing " + _count + " objects with " + _depth + " levels of embedded objects:"); Clean(); System.Console.WriteLine(); System.Console.WriteLine(" - primitive object with int field"); Open(Configure()); StoreSimplest(); objectContainer.Ext().Purge(); Close(); Open(Configure()); StartTimer(); IQuery query = objectContainer.Query(); query.Constrain(typeof(SimplestItem)); query.Descend("_id").Constrain(1); IList result = query.Execute(); SimplestItem simplestItem = (SimplestItem)result[0]; StopTimer("Querying SimplestItem: " + simplestItem._id); Close(); Open(Configure()); System.Console.WriteLine(); System.Console.WriteLine(" - object with string field"); Store(); objectContainer.Ext().Purge(); Close(); Open(Configure()); StartTimer(); query = objectContainer.Query(); query.Constrain(typeof(Item)); query.Descend("_name").Constrain("level1/2"); result = query.Execute(); Item item = (Item)result[0]; StopTimer("Querying object with string field: " + item._name); Close(); Clean(); Open(Configure()); System.Console.WriteLine(); System.Console.WriteLine(" - object with StringBuilder field"); StoreWithStringBuffer(); objectContainer.Ext().Purge(); Close(); Open(Configure()); StartTimer(); query = objectContainer.Query(); query.Constrain(typeof(ItemWithStringBuilder)); query.Descend("_name").Constrain(new StringBuilder("level1/2")); result = query.Execute(); ItemWithStringBuilder itemWithSB = (ItemWithStringBuilder)result[0]; StopTimer("Querying object with StringBuilder field: " + itemWithSB._name); Close(); Clean(); Open(Configure()); System.Console.WriteLine(); System.Console.WriteLine(" - object with int array field"); StoreWithArray(); objectContainer.Ext().Purge(); Close(); Open(Configure()); StartTimer(); query = objectContainer.Query(); query.Constrain(typeof(ItemWithArray)); IQuery idQuery = query.Descend("_id"); idQuery.Constrain(1); idQuery.Constrain(2); idQuery.Constrain(3); idQuery.Constrain(4); result = query.Execute(); ItemWithArray itemWithArray = (ItemWithArray)result[0]; StopTimer("Querying object with Array field: [" + itemWithArray._id[0] + ", " + +itemWithArray._id[1] + ", " + +itemWithArray._id[2] + ", " + +itemWithArray._id[0] + "]"); Close(); Clean(); Open(Configure()); System.Console.WriteLine(); System.Console.WriteLine(" - object with ArrayList field"); StoreWithArrayList(); objectContainer.Ext().Purge(); Close(); Open(Configure()); StartTimer(); query = objectContainer.Query(); query.Constrain(typeof(ItemWithArrayList)); query.Descend("_ids").Constrain(1).Contains(); result = query.Execute(); ItemWithArrayList itemWithArrayList = (ItemWithArrayList)result[0]; StopTimer("Querying object with ArrayList field: " + itemWithArrayList._ids.ToString()); Close(); }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: Init private void Init() { _filePath = "performance.db4o"; // amount of objects _count = 10000; // depth of objects _depth = 3; _isClientServer = false; }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: Configure private IConfiguration Configure() { IConfiguration config = Db4oFactory.NewConfiguration(); return config; }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: SimplestItem public class SimplestItem { public int _id; public SimplestItem _child; public SimplestItem() { } public SimplestItem(int id, SimplestItem child) { _id = id; _child = child; } }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: ItemWithStringBuilder public class ItemWithStringBuilder { public StringBuilder _name; public ItemWithStringBuilder _child; public ItemWithStringBuilder() { } public ItemWithStringBuilder(StringBuilder name, ItemWithStringBuilder child) { _name = name; _child = child; } }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: ItemWithArray public class ItemWithArray { public int[] _id; public ItemWithArray _child; public ItemWithArray() { } public ItemWithArray(int[] id, ItemWithArray child) { _id = id; _child = child; } }</pre> <pre class="prettyprint" xml:space="preserve">QueryPerformanceBenchmark.cs: ItemWithArrayList public class ItemWithArrayList { public ArrayList _ids; public ItemWithArrayList _child; public ItemWithArrayList() { } public ItemWithArrayList(ArrayList ids, ItemWithArrayList child) { _ids = ids; _child = child; } }</pre> </p> <p MadCap:conditions="Global.Primary:net" /> <p>Results from the test machine:</p> <p><i>- primitive object with int field</i> </p> <p><i>Store 30000 objects: 1878ms</i> </p> <p><i>Querying SimplestItem: 1: 425ms</i> </p> <p><i></i> </p> <p><i> - object with String field</i> </p> <p><i>Store 30000 objects: 2599ms</i> </p> <p><i>Querying object with String field: level1/2: 436ms</i> </p> <p><i></i> </p> <p><i> - object with StringBuffer field</i> </p> <p><i>Store 30000 objects: 5658ms</i> </p> <p><i>Querying object with StringBuffer field: level1/2: 3489ms</i> </p> <p><i></i> </p> <p><i> - object with int array field</i> </p> <p><i>Store 30000 objects: 2487ms</i> </p> <p><i>Querying object with Array field: [1, 2, 3, 1]: 1777ms</i> </p> <p><i></i> </p> <p><i> - object with ArrayList field</i> </p> <p><i>Store 30000 objects: 5302ms</i> </p> <p><i>Querying object with ArrayList field: [1, 2, 3, 4]: 3796ms</i> </p> <p MadCap:conditions="Primary.Online">Download example code:</p> <p MadCap:conditions="Primary.Online"> <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="queryperformancecs.zip">c# </a> </MadCap:conditionalText> </p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>