Benchmark: Scripting Dictionary vs XData

Discussion in 'AutoCAD' started by wivory, Jun 9, 2004.

  1. wivory

    wivory Guest

    For the community knowledgebank...

    I was looking to streamline a routine that is called intensively in my program so as to cut down the overall processing time. The method I devised would entail keeping some information about each polyline in the drawing and accessing it later in the program (multiple times).

    The obvious avenue was XData but I wondered if a Scripting Dictionary might be faster. As I was trying to squeeze as much performance as possible out of the routine I decided to write both methods and compare them. I used the CCRP High Performance Timer Objects as they are specifically optimized for benchmarking and don't require a form to live on.

    Before I share the results, let me point out one "gotcha". As part of the benchmarking I kept a count of how many times the read and write routines were invoked. I noticed that the Dictionary routines were invoked more times than the XData routines. This didn't make sense to me as they were supposed to be performing the same function (and the end results did appear to be the same) so I endeavoured to isolate the difference. I spent *hours* on this before finally working it out - one part of my main code involved using the Copy method to copy one polyline to another. With the XData method the information was transferred to the copy, but of course with the Dictionary method it was not. Subsequent logic that tested for the presence of the information caused the two paths to diverge. You should decide on a case-by-case basis whether you want copied entities to inherit the "information" from the original.
    [pre]
    Invocations XData Dictionary
    Write 182 41ms 15ms
    Read 680 6304ms 769ms
    [/pre]
    As well as being faster, the Dictionary approach was significantly easier to code. For XData the code had to determine the data type of the data to be stored and set up the XDataType structure accordingly. This was more complex when storing array information. For the Dictionary I simply used the Entity Handle concatenated with the "Property Name" (something made up by the programmer) as the dictionary key and stored the required value against it.

    Hope this is useful.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Jun 9, 2004
    #1
  2. wivory

    manoj_vest Guest

    Hi Wayne,

    I am trying to store a string with some objects. My concern is that when ever the object is copied and created a new instance from the old one, the string stored with the previous instance should not be copied in new object. For this I tried to store the string in XData, but when ever I am copying the entity which has XData, the value of XDAta too is get copied. By seeing your post I think that my task can be achieved by implementing with the dictionary. So can you provide me a small pice of code how to store the string in the dictionary of an object.

    Thanks in advance

    manoj
     
    manoj_vest, Jun 9, 2004
    #2
  3. wivory

    perry Guest

    Uuuu, dumb question but, what is a "scripting dictionary" as opposed to
    regular dictionary routines I use?
     
    perry, Jun 9, 2004
    #3
  4. True but speed isn't the only issue. XData can be stored directly on an
    entity while external information must be manually synched. XData can
    include information which updates automatically as the entity changes.
    Again, information must be manually cynched with dictionaries.

    I'm not casting aspersions upon your work or observations. I just wanted
    to make a counterpoint to ensure that people take the time to evaluate
    which tool would be best for their particular programming needs.
     
    Frank Oquendo, Jun 9, 2004
    #4
  5. Microsoft Scripting Dictionary available when you add a reference to the
    Microsoft Scripting Runtime. This has nothing to do with AutoCAD - it is a
    programming object kinda like a Collection on steriods

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Jun 9, 2004
    #5
  6. wivory

    perry Guest

    Aahh, interesting. Are there any examples on this?
     
    perry, Jun 9, 2004
    #6
  7. wivory

    MP Guest

    From: Tony Tanzillo (tony.tanzillo_at_caddzone_dot_com)
    Subject: Re: Function to return collection


    View this article only
    Newsgroups: autodesk.autocad.customization.vba
    Date: 2003-04-03 16:08:44 PST


    Use a dictionary, and populate it with keys that
    are names of the collections and values that are
    the collections themselves.

    Then, you can just reference the given collection
    by a string key (which is its name).

    ' ---------------------------------------------------
    ' Requires reference to scripting runtime:

    Dim collDict As Scripting.Dictionary


    Public Sub InitCollDict()
    Set collDict = New Scripting.Dictionary
    collDict.Add "LAYERS", ThisDrawing.Layers
    collDict.Add "BLOCKS", ThisDrawing.Blocks
    collDict.Add "LINETYPES", ThisDrawing.Linetypes

    ' and so on ....

    End Sub

    Public Sub Test()
    InitCollDict
    Debug.Print "Blocks.Count = " & collDict("BLOCKS").Count
    End Sub
     
    MP, Jun 9, 2004
    #7
  8. wivory

    wivory Guest

    Sure. Here is what I ended up with.

    Code:
    ' Add a reference to the Microsoft Scripting Runtime
    Dim CustomProperties As New Dictionary
    Const SEPARATOR As String * 1 = "~"
    '
    Private Sub PropertyLet(Entity As AcadEntity, PropertyName As String, PropertyValue As Variant)
    If CustomProperties.Exists(Entity.Handle & SEPARATOR & PropertyName) Then
    CustomProperties(Entity.Handle & SEPARATOR & PropertyName) = PropertyValue
    Else
    CustomProperties.Add Entity.Handle & SEPARATOR & PropertyName, PropertyValue
    End If
    End Sub
    '
    Private Function PropertyGet(Entity As AcadEntity, PropertyName As String) As Variant
    If CustomProperties.Exists(Entity.Handle & SEPARATOR & PropertyName) Then PropertyGet = CustomProperties(Entity.Handle & SEPARATOR & PropertyName)
    End Function
    '
    Private Sub Example()
    PropertyLet GeneralLine, "Material", "Elastic"
    PropertyLet GeneralLine, "AssociatedPoint", OtherLine.StartPoint
    MsgBox "This line is made of " & PropertyGet(GeneralLine, "Material")
    End Sub
    
    Regards

    Wayne
     
    wivory, Jun 10, 2004
    #8
  9. wivory

    wivory Guest

    Right you are. In my case speed *was* the issue, plus the information was static (ie not required to scale) and of a transient nature so it was not required once the macro finished.

    As you say, other people with different requirements may be better suited to XData. Thanks for clarifying where I'm coming from.

    Regards

    Wayne
     
    wivory, Jun 10, 2004
    #9
  10. wivory

    manoj_vest Guest

    But I think my requirement was different than the yours one and it cannot be served vy scripting dictionary. I wanted to store some string with acad entity e.g. line. When ever a user copies the line manually using the copy command of autocad, every thing except the string stored with the line should be copied. The string stored should persist in different session of AutoCAD.
     
    manoj_vest, Jun 10, 2004
    #10
  11. I use XData which I don't want to be copied with the object so what I have done is undefined the copy command and created my own in VBA that removes the paticular XData. This does not take into consideration other commands like CopyClip. Also because my new copy command is in VBA I have lost the dragging effect & ortho control which I hope to rectify with Tony Tanzillo's AcadX DynamicGraphics class.

    Regards - Nathan
     
    Nathan Taylor, Jun 10, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.