Which method is more optimized?

Well, I need to create an xml file and write tags to it. I created the xml file as normal creation of a text file, but with the extension .xml and using these operators

First approach

a.writeline("<root>") if (check_boxapp.value = true ) then a.writeline("<condition>value</condition>") end if if (check_boxname.value = true ) then a.writeline("<condition>value1</condition>") end if 

like these, and I have about 50 if the statements are .20 for <condition> tags and another 5 for someother tag and another 2 for naming the tag. So I can’t go for the status of a switch case. My question is that this slows down the execution of the vba macro, because for everyone, if statment Im gets access to the file. I check the condition and access the file, so this slows down the performance. This is my first approach.

Second approach

creating an array and tracking the status of the flags in the array, and then at the end, loop through the array, if it has 1, then use writeline else dont. sort of

 if chechk_box.value = true then a(i) = 1 end if and at the end for i = 1 to 20 if(a(i)=1) then a.writeline("something") end if next i for i = 1 to 10 if(a(i)=1) then a.writeline("something") end if next i 

Maybe I think I have about 6 cycles. In this approach, I create an array and use 50 statuses and then 6 for loops. I believe that creating an array eats memory, as well as the code is small in comparison with the first one, and also a little difficult to understand.

But I'm not sure which one would be faster. Please help with this or any other smart way, much appreciated

+4
source share
2 answers

The fact that a very small number of I / O operations and worry about recording time seems to be an unnecessary problem, since any of them should be in the low millisecond range.

Here is another approach you can take to simplify the task by listing the controls themselves;

 '//checkbox example Dim Ctrl As Control For Each Ctrl In Me.Controls if TypeName(Ctrl) = "CheckBox" Then '//look at each checkbox '//you could use the controls .name to pull out its node value '//by using a naming convention like "chk_XML_NodeValue" + mid$(Ctrl.Name, 9) '//or by putting the node value in the Tag propery of the control (in the designer or code) if (Ctrl.value) then a.writeline "<condition>" & Ctrl.Tag & "</condition>" end if End If Next 
+1
source

Create your in-memory file using the MSXML object model, and then upload the whole thing to disk using DOMDocument.Save. I assume VBA is here - you are using "WriteLine" in your example, but VBA does not have a "WriteLine" method.

Example:

 Dim xmlDoc As Object ' MSXML2.DOMDocument60 Dim pi As Object ' MSXML2.IXMLDOMNode Dim rootelem As Object ' MSXML2.IXMLDOMNode Dim valueNode As Object ' MSXML2.IXMLDOMNode ' create new MSXML Document Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") ' add XML declaration at top of file Set pi = xmlDoc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'") xmlDoc.appendChild pi ' create root element Set rootelem = xmlDoc.createElement("root") xmlDoc.appendChild rootelem If check_boxapp.value then Set valueNode = xmlDoc.createElement("condition") valueNode.nodeTypedValue = "value" rootelem.appendChild valueNode End If If check_boxname.value then Set valueNode = xmlDoc.createElement("condition") valueNode.nodeTypedValue = "value1" rootelem.appendChild valueNode End If ' save to disk xmlDoc.Save "C:\MyFile.xml" 

You now have a valid XML file. It would be easy to tweak this code (using the code provided by Alex K) to skip your checkboxes and write XML.

0
source

Source: https://habr.com/ru/post/1379118/


All Articles