Click to See Complete Forum and Search --> : Optimize my code, Please
Sgt-Peppa
May 26th, 2003, 02:48 AM
Hey, i am pretty new to C# and wrote some code for work. Before turnimg that in could you please take a alook and make suggestions to optimize it. I am pretty sure that there are ways to make this code a lot better. Thanx in advance,
Stephan
XmlDocument source = new XmlDocument(); //new Instance of xml-Document
source.Load(@"C:\cvs\TeccomPOC\xml\regExp.xml"); // select the xml source
XmlNode docElement = source.DocumentElement; // new Instance of Node
bool found =false; // Variable to keep track if a regexpresion has been found
bool goon = true; //Variable to break out of a foreach loop
int count =0; // keeping track of the amount of loops
int max = docElement.ChildNodes.Count; // count the childnodes
while (goon ==true && count<=max) // do as long as goon is true and the count has not reached the amount of childnodes
{
foreach(XmlNode n2 in docElement.ChildNodes) //for every childnode in <doctypes>
{
if(n2.Name =="type") //if childnode is called type
{
foreach(XmlNode n3 in n2.ChildNodes) // for every childnode in <type>
{
if (count ==max)
{
header = "UNKNOWN";
}
if (found == true) // has the regexpresion been found
{
header = n3.InnerText; // assign the name of the source to the header
found = false;
goon=false;
break;
}
if(n3.Name == "regexpression") // if childnode is called regexpression
{
string tmp = n3.InnerText; // assign the tag-value to tmp
if(rest.IndexOf(tmp) !=-1) //compare the tmp to the rest of the file
{
found =true; // if the regexp is found set found to true
}
}
}
count = count +1; // keeping track of the amounts of loops
}
}
}
And heres my xml Structure:
<doctypes>
<type>
<regexpression>test::</regexpression>
<name>test1</name>
<containsbin>no</containsbin>
</type>
<type>
<regexpression>testing::</regexpression>
<name>test2</name>
<containsbin>no</containsbin>
</type>
</doctypes>
CornedBee
May 26th, 2003, 02:53 AM
Tip1: remove the immense indentation when posting :)
Sgt-Peppa
May 26th, 2003, 03:02 AM
done
BTW: Sorry for that
CornedBee
May 26th, 2003, 09:47 AM
Looks quite ok, except that it completly relies on a valid XML document (do you have a DTD defined to check?).
I don't completly understand the count thing, but I guess you have a reason for that. But you can write
++count;
instead of
count = count + 1;
Sgt-Peppa
May 27th, 2003, 03:05 AM
Thanx for taking time to look over it! And yap theres a reason for that count. But thanks for your ++ hint. I know that from c, but it`s twisted around there: x ++; :p
So thanx again, Stephan
EDIT: Oh and yes, I do have a DTD to check my xml
CornedBee
May 27th, 2003, 05:00 AM
Both x++ and ++x are valid, I tend to use the latter because it is faster in some special cases in C++ (which therefore should be called ++C ;) )
Sgt-Peppa
May 27th, 2003, 06:10 AM
Oh äh, BTW, do you know of a good validation tool for my xml and dtd?
CornedBee
May 27th, 2003, 06:49 AM
I don't know about C#, but in Java I can set the XmlDomParserFactory to create a validating parser. Then, when I call the parser's parse method it throws a SAXException if the document is not valid.
I think XmlDocument should provide something similar.
Sgt-Peppa
May 27th, 2003, 08:03 AM
Just if you are interested:
I couldnt find anything for xmlDocument but for xmlReader.
Heres some code you might find Interesting:
// this in my main
XmlTextReader r = new XmlTextReader(@"C:\cvs\test\xml\regExp.xml");
XmlValidatingReader v = new XmlValidatingReader(r);
v.ValidationType = ValidationType.DTD;
v.ValidationEventHandler += new ValidationEventHandler (handle_validation);
while(v.Read());
public static void handle_validation(object s, ValidationEventArgs a)
{
Console.WriteLine("Severity:{0}", a.Severity);
Console.WriteLine("Message:{0}", a.Message);
}
Maybe you`ll need that sometimes!
Thanx for all your help, Stephan
CornedBee
May 27th, 2003, 09:25 AM
Thanks.
And I prefer the Java way :)
nemaroller
Jun 8th, 2003, 10:29 AM
As a side note:
++Counter and Counter++, could be two seperate ways of incrementing depending on how you implement it... in your code, it doesn't matter, but if you were using it in a for-loop... (for int i =0; i <10; i++)
++i increments before the loop...
i++ increments after the loop...
Just had to say that in case anyone else decides to use it in a loop... and it actually executes faster than counter=counter+1, since two copies of counter would be created at compile time with counter=counter+1, instead of just one with counter++.
CornedBee
Jun 9th, 2003, 04:36 AM
As a matter of fact, both ++i and i++ execute at the same time when in a for-loop: after the loop body.
And considering two "types" of increment: that would be a huge design error, lead to terrible bugs and be completly unmaintainable.
CornedBee
Jun 9th, 2003, 04:37 AM
Also I'd like to mention that with primitive types, i=i+1 and i++ amount to the same code using an optimizing compiler, and both i=i+1 and i++ have a temporary object when using overloaded operators on a class object.
Only ++i doesn't use a temp object.
nemaroller
Jun 9th, 2003, 08:30 AM
As a matter of fact, both ++i and i++ execute at the same time when in a for-loop: after the loop body. - CornedBee
Um.... no, they don't...
"And considering two "types" of increment: that would be a huge design error, lead to terrible bugs and be completly unmaintainable" - CornedBee
Perhaps implementing it would, but not knowing the difference would definitely lead to catastrophe...
"If ++ is applied to a number variable, it simply adds one to the variable's value.
There are two alternative representations: x++ (called 'post-increment') and ++x (called pre-increment). If 'x++' is a statement on its own pre- and post-increment instructions have exactly the same effect. If 'x++' is part of a larger expression being evaluated, pre-increment cause the value to be incremented before the expression is evaluated, while post-increment causes the increment to come after evaluation. In my opinion, a program is easier to understand if it is written in such a way that pre-increment and post-increment would be identical. This saves the reader needing to work out the order in whcih things happen. "
.Net
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=610
C++
http://www.kevinboone.com/ctut_opref.html
http://www.technipal.com/cpp/controlstructuresprint.html
Java
http://java.about.com/library/tutorials/bltut-013k.htm
As far as saying x = x + 1 is optimized as well as x+=1
Optimize Assignments
Use exp += val instead of exp = exp + val. Since exp can be arbitrarily complex, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of exp, and many times this is not needed. The first statement can be optimized far better than the second, since the JIT can avoid evaluating the exp twice.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp
CornedBee
Jun 10th, 2003, 06:58 AM
As far as saying x = x + 1 is optimized as well as x+=1
Sorry, was thinking C++ there.
Um.... no, they don't...
Yes they do.
// A;
for(int i=0;i<10;++i)
generic_print(i);
// B:
for(int i=0;i<10;i++)
generic_print(i);
Results:
Java:
0123456789
0123456789
C++:
0123456789
0123456789
I have no reason to doubt that the results are the same in C#.
If ++i incremented before the loop, the result would be
12345678910
for the first line.
If 'x++' is part of a larger expression being evaluated, pre-increment cause the value to be incremented before the expression is evaluated, while post-increment causes the increment to come after evaluation.
I never said anything about that. This is not what I consider two types of increment. I was responding to what you said here:
++Counter and Counter++, could be two seperate ways of incrementing depending on how you implement it
Which struck me as odd, since I thought you might mean something like this (C++):
class my
{
int val1, val2;
// preincrement-operator
const my &operator ++() {
++val1;
return *this;
}
// post-increment-operator
my operator ++(int) {
my t(*this);
++val2;
return t;
}
};
Which would really be horrible.
nemaroller
Jun 10th, 2003, 09:27 AM
Well, that Java pre- post increment is strange, because I swore it works like that in C++ also (have to wait to get home to try it though)...
And no, I wasn't referring to overloading the increment operator...
Thank god I don't do much Java /C++ programming, but seriously, I must have somehow thought the pre-increment operator worked in for-loops in addition to assignments like y = ++x;
shunt
Jul 2nd, 2003, 01:53 PM
Hi guys,
I don't see anything wrong with your C# abilities there, however, your use of the xml objects is incorrect.
By using the SelectNodes function of the xmldocument object and using the XPath query language, you can cut your code down to 3 or 4 lines rather than all the nesting and counting.
If you want all the nodes that have a name of regexpression then use the XPath "//regexpression" in the SelectNodes function. This will return a collection of type XMLNodeList containing all elements named regexpression in the document.
Look into it! As the architect at my work, by developers get things chopped off for using loops like that! :mad:
If you explain exactly what you are trying to achieve with these loops, I will send you an equivalent code and XPath!
:D
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.