<rdf:RDF
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xml:base='http://www.stefanrufer.ch/snipsnap/rdf'>
    <s:Snip rdf:about='http://www.stefanrufer.ch/snipsnap/rdf#Virtual+Brain/Java/List+Speed'
         s:cUser='stefan'
         s:oUser='stefan'
         s:mUser='stefan'>
        <s:name>Virtual Brain/Java/List Speed</s:name>
        <s:content>1 Idea&#xD;&#xA;&#xD;&#xA;Think about an application that offers to change password and upon checking the new password for strength we consult a dictionary in memory to provide quick response or to read it from disk every time a user changes a password. &#xD;&#xA;&#xD;&#xA;At first glance, I don&apos;t know if it&apos;s worth to preload a dictionary in memory.          Changing passwords happens seldom and a user might feel ok if he has to wait a few seconds for this to complete if he get&apos;s a nice message saying that his password is checked for strength. We can release this 80 megs of memory in the JVM :-)&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;1 Test Setup&#xD;&#xA;&#xD;&#xA;An {link:eclipse|http://eclipse.org} project is attached to this page if you want to try it by yourself (see &quot;lasttest.zip&quot; link in the grey box).&#xD;&#xA;&#xD;&#xA; - Source: text-file with alphabetically ordered dictionary words                       (15MB, ca. 1.5 Mio entries)&#xD;&#xA; - Fill lists sequentially from file, therefore ArrayList and                            LinkedList will have alphabetical order as well&#xD;&#xA; - Search for a string &apos;zzzz&apos; as this is assumed to be near the end                     of the dictionarys entries&#xD;&#xA; - Search time measured in JUnit test-setup with System.currentTimeMillis               before and after call to Collection.contains(&quot;zzzz&quot;)&#xD;&#xA; - Measured on: workstation (3GHz/1GB) under WinNT, JDK 1.4.2_04                     &#xD;&#xA;&#xD;&#xA;&#xD;&#xA;1 Results&#xD;&#xA;&#xD;&#xA;This is the result of a 2nd run of the test on my machine. The test runs only once (hence there&apos;s no warm up phase). &#xD;&#xA;&#xD;&#xA;{code:none}&#xD;&#xA;Init with 1450252 entries using a java.util.Vector&#xD;&#xA;Time:            3125 ms&#xD;&#xA;Used Heap Start: 300424 bytes&#xD;&#xA;Used Heap End:   86314992 bytes&#xD;&#xA;Consumed Heap:   86014568 bytes&#xD;&#xA;Time:            0 ms&#xD;&#xA;Time:            62 ms&#xD;&#xA;Init with 1450125 entries using a java.util.HashSet&#xD;&#xA;Time:            7892 ms&#xD;&#xA;Used Heap Start: 442920 bytes&#xD;&#xA;Used Heap End:   115511600 bytes&#xD;&#xA;Consumed Heap:   115068680 bytes&#xD;&#xA;Time:            0 ms&#xD;&#xA;Time:            0 ms&#xD;&#xA;Init with 1450252 entries using a java.util.ArrayList&#xD;&#xA;Time:            1735 ms&#xD;&#xA;Used Heap Start: 4875040 bytes&#xD;&#xA;Used Heap End:   88911368 bytes&#xD;&#xA;Consumed Heap:   84036328 bytes&#xD;&#xA;Time:            0 ms&#xD;&#xA;Time:            62 ms&#xD;&#xA;Init with 1450252 entries using a java.util.LinkedList&#xD;&#xA;Time:            1906 ms&#xD;&#xA;Used Heap Start: 4875568 bytes&#xD;&#xA;Used Heap End:   107856872 bytes&#xD;&#xA;Consumed Heap:   102981304 bytes&#xD;&#xA;Time:            0 ms&#xD;&#xA;Time:            204 ms&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;1 Comments&#xD;&#xA;&#xD;&#xA; i. HashSet takes longest to fill. That&apos;s about clear as we have to deal with many hash calculations and - I guess - some collisions because of the nature of the dictionary words.&#xD;&#xA; i. Finding the entry &quot;zzzz&quot; in all types of lists is really fast. Consider LinkedList where we have to ~~traverse~~ the whole list up to the end (at least 1.4 Mio entries) before we have a match. This results in 0.15 microseconds per &quot;next-compare&quot; step (204ms / 1&apos;400&apos;000 step). This in turn means that my PC uses roughly 450 CPU cycles for one next-compare step (3*10^9 cycles/sec * 0.15*10^-6 sec/step). &#xD;&#xA; i. Is this nice work of the JIT engineers? I say yes!&#xD;&#xA;</s:content>
        <s:mTime>2004-10-06 16:18:42.82</s:mTime>
        <s:cTime>2004-10-06 15:55:01.705</s:cTime>
        <s:comments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:snipLinks>
            <rdf:Bag>
                <rdf:li rdf:resource='http://www.stefanrufer.ch/snipsnap/rdf#Virtual Brain/Java'/>
                <rdf:li rdf:resource='#snipsnap-index'/>
                <rdf:li rdf:resource='http://www.stefanrufer.ch/snipsnap/rdf#Virtual Brain'/>
                <rdf:li rdf:resource='http://www.stefanrufer.ch/snipsnap/rdf#Virtual+Brain/Linux'/>
                <rdf:li rdf:resource='#stefan'/>
                <rdf:li rdf:resource='#Unternehmungen'/>
                <rdf:li rdf:resource='http://www.stefanrufer.ch/snipsnap/rdf#Virtual+Brain'/>
                <rdf:li rdf:resource='http://www.stefanrufer.ch/snipsnap/rdf#My Projects/Geigenhof Pool'/>
            </rdf:Bag>
        </s:snipLinks>
        <s:attachments>
            <rdf:Bag>
                <rdf:li>
                    <s:Attachment rdf:about='http://www.stefanrufer.ch/snipsnap/space/Virtual+Brain/Java/List+Speed/listtest.zip'
                         s:fileName='listtest.zip'
                         s:contentType='application/zip'
                         s:size='123127'>
                        <s:date>Wed Oct 06 16:14:36 CEST 2004</s:date>
                    </s:Attachment>
                </rdf:li>
            </rdf:Bag>
        </s:attachments>
    </s:Snip>
</rdf:RDF>

