|
-
May 26th, 2003, 02:48 AM
#1
Thread Starter
Hyperactive Member
Optimize my code, Please
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
Code:
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:
Code:
<doctypes>
<type>
<regexpression>test::</regexpression>
<name>test1</name>
<containsbin>no</containsbin>
</type>
<type>
<regexpression>testing::</regexpression>
<name>test2</name>
<containsbin>no</containsbin>
</type>
</doctypes>
Last edited by Sgt-Peppa; May 26th, 2003 at 03:01 AM.
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 26th, 2003, 02:53 AM
#2
Tip1: remove the immense indentation when posting
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 26th, 2003, 03:02 AM
#3
Thread Starter
Hyperactive Member
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 26th, 2003, 09:47 AM
#4
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;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 27th, 2003, 03:05 AM
#5
Thread Starter
Hyperactive Member
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 ++;
So thanx again, Stephan
EDIT: Oh and yes, I do have a DTD to check my xml
Last edited by Sgt-Peppa; May 27th, 2003 at 04:45 AM.
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 27th, 2003, 05:00 AM
#6
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 )
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 27th, 2003, 06:10 AM
#7
Thread Starter
Hyperactive Member
Oh äh, BTW, do you know of a good validation tool for my xml and dtd?
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 27th, 2003, 06:49 AM
#8
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 27th, 2003, 08:03 AM
#9
Thread Starter
Hyperactive Member
Just if you are interested:
I couldnt find anything for xmlDocument but for xmlReader.
Heres some code you might find Interesting:
Code:
// 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
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 27th, 2003, 09:25 AM
#10
Thanks.
And I prefer the Java way
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 8th, 2003, 10:29 AM
#11
I wonder how many charact
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++.
-
Jun 9th, 2003, 04:36 AM
#12
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 9th, 2003, 04:37 AM
#13
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 9th, 2003, 08:30 AM
#14
I wonder how many charact
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/tut...utorial_id=610
C++
http://www.kevinboone.com/ctut_opref.html
http://www.technipal.com/cpp/control...uresprint.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/de...etperftips.asp
Last edited by nemaroller; Jun 9th, 2003 at 08:49 AM.
-
Jun 10th, 2003, 06:58 AM
#15
As far as saying x = x + 1 is optimized as well as x+=1
Sorry, was thinking C++ there.
Yes they do.
Code:
// 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++):
Code:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 10th, 2003, 09:27 AM
#16
I wonder how many charact
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;
-
Jul 2nd, 2003, 01:53 PM
#17
Fanatic Member
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!
If you explain exactly what you are trying to achieve with these loops, I will send you an equivalent code and XPath!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|