|
-
Jun 18th, 2007, 06:03 PM
#1
Thread Starter
New Member
[RESOLVED]Why is this so slow?
I have removed all the 'console.writeline' commands from the for..next loop.
The program loads a text file of any size which in this case contains about 5000 lines. It itterates through them looking for specific information.
This can take two or three minutes at the absolute minimum. Is there a reason for this? Or is it just very much slower when in debug mode?
Or, as I am new to VB, have I missed something fairly fundemental?
Code:
If RB2_time.Checked = False Then
' parsing an npc - all damage against this npc
If ComboB2_all_combatants.SelectedItem.ToString.Contains(" ") = True Then
Console.WriteLine(RTB_Selected_File.Lines.GetUpperBound(0))
length_text_file = RTB_Selected_File.Lines.GetUpperBound(0)
For count1 = 0 To length_text_file
current_line = RTB_Selected_File.Lines(count1)
If (current_line.Contains(ComboB2_all_combatants.SelectedItem.ToString) = True) And (RTB_Selected_File.Lines(count1).Contains(non_melee) = True Or RTB_Selected_File.Lines(count1).Contains(melee_hit)) Then
'Console.WriteLine("test " + count1.ToString + " " + RTB_Selected_File.Lines(count1).ToString)
If current_line.EndsWith(".") = True Or current_line.EndsWith("!") = True Then
current_line = current_line.Remove(current_line.Length - 1, 1)
End If
current_line = current_line.Remove(0, 27)
' ******************************
For count2 = 0 To ComboB2_all_combatants.Items.Count - 1
' The code checks that the line of text has both combatants. This is done by ensuring that they are not the same name.
If current_line.StartsWith(display_name_Array(count2)) And display_name_Array(count2).ToString <> ComboB2_all_combatants.SelectedItem.ToString Then
total_damage = 0
count3 = current_line.LastIndexOf("for")
total_damage = Val(total_damage_Array(count2)) + Val(Mid(current_line, count3 + 4, 6))
total_damage_Array(count2) = total_damage
' The code around here checks previous line to see if it was a critical attack
' previous_line critical_hit critical_blast critical_undead
ElseIf current_line.StartsWith(ComboB2_all_combatants.SelectedItem.ToString) And current_line.Contains(display_name_Array(count2)) = True And display_name_Array(count2).ToString <> ComboB2_all_combatants.SelectedItem.ToString Then
total_damage = 0
End If
Next
' ******************************
'count2 = 0
'Do While attack_melee_casterArray(count2) <> ""
' If current_line.Contains(attack_melee_casterArray(count2)) = True Then
' ' updates specific attack type
' End If
' count2 += 1
'Loop
Else
End If
Next
' ===================================================================
' adds the data to the display grid, names, damage, duration, dps etc
Data_ParseTable.Rows.Clear()
For count1 = 0 To total_damage_Array.GetUpperBound(0)
If display_name_Array(count1) <> "" Then
Data_ParseTable.Rows.Add(display_name_Array(count1), Val(total_damage_Array(count1)))
End If
Next
Else ' parse a Player's toon- this is known because there is at least one space in the name.
Console.WriteLine("Parsing a player's toon")
End If
Else ' parse from time to time
End If
Last edited by Writers Block; Jun 19th, 2007 at 04:39 AM.
-
Jun 18th, 2007, 06:13 PM
#2
Re: Why is this so slow?
I dont know wether this affects speed or not but I see you are evaluating booleans like this:
VB.Net Code:
If bln = True Then
End If
See what this does is taking one boolean variable (in this case 'bln') and comparing it with another boolean variable (in this case 'True') which will return a third boolean variable indicating if they match or not.
You could just be writing it like this instead:
or to check if its false:
Another thing Ive noticed is you are using Mid() and Val().
Instead of Mid, use the Substring method that is a member of the String type.
And instead of Val, use the appropriate convert function: Cint()/Convert.ToInt32(), Cdbl()/Convert.ToDouble() etc etc.
Also if you havent done so already, turn Option Strict ON.
Any other slowness could be due to your codes design, rethink the entire idea, could it be done in a simpler way? Also, reading through big files usually take some time.
-
Jun 18th, 2007, 06:27 PM
#3
Hyperactive Member
Re: Why is this so slow?
It seems to me that the length of time encountered is quite normal considering your code and the fact that the text files may be 5000 lines...
Like Atheist said, you should rethink your code, and if you cant get the time down maybe you should use a thread if its appropriate...
-
Jun 18th, 2007, 06:30 PM
#4
Re: Why is this so slow?
You are doing lots of string manipulations, they will take lots of time, and there may be no good way to avoid it....without rethinking things. Reading that many strings takes very little, but parsing them, calling methods on them, concatenating them, etc., all of these are slow.
If you are creating the files, perhaps you could create them in such a way that they are not strings. This could be done in a few ways. One is to read the entire file in, and take the information out of the strings and put the relevant info into structures, then hold a List of the structures. This would move the operations up front, and would be much faster when traversing the array. Of course, you may not be able to do anything with the file.
My usual boring signature: Nothing
 
-
Jun 18th, 2007, 06:39 PM
#5
Thread Starter
New Member
Re: Why is this so slow?
Thank you for all your replies. Trying some ideas but it seems like I am stuck with the speed or lack of it.
-
Jun 18th, 2007, 09:26 PM
#6
Re: Why is this so slow?
There were no replies when I made my post, so I didn't see what the others had added. Atheists point about booleans is correct. It is faster not to have the = sign in there, since that is simply redundant.
What is killing you is the strings themselves. You really need to get away from them by some means. If you have control over the files (if they are not being created by a different process), then that is where you need to start. There are alternatives to text files that will be faster.
My usual boring signature: Nothing
 
-
Jun 18th, 2007, 09:45 PM
#7
Re: Why is this so slow?
Trust me on this: None of that is the problem. The problem is you have all the data you are messing with in controls. Store the data in an array and manipulate it instead. Transferring data to and from a textbox or a combobox is extremely time consuming. That whole loop with arrays would take abou 1/2 sec.
-
Jun 19th, 2007, 04:38 AM
#8
Thread Starter
New Member
[RESOLVED] Re: Why is this so slow?
 Originally Posted by Lord Orwell
Trust me on this: None of that is the problem. The problem is you have all the data you are messing with in controls. Store the data in an array and manipulate it instead. Transferring data to and from a textbox or a combobox is extremely time consuming. That whole loop with arrays would take abou 1/2 sec.
Thank you everyone who posted, all have helped with my code.
But yes, changing the 'item' access over to arrays made a massive difference.
I have posted the code I have now.
It's funny. I was lyeing in bed this morning thinking that when the file is loaded the program searches through looking for specific information. That takes less than a second, so why should this particular itteration take so long?
Now I know, thx again.
Code:
If Not RB2_time.Checked Then
' parsing an npc - all damage against this npc
If ComboB2_all_combatants.SelectedItem.ToString.Contains(" ") Then
number_combo_index = ComboB2_all_combatants.SelectedIndex
'Console.WriteLine(RTB_Selected_File.Lines.GetUpperBound(0))
length_text_file = RTB_Selected_File.Lines.GetUpperBound(0)
Console.WriteLine("start")
For count1 = 0 To length_text_file
current_line = loaded_file_text_box_Array(count1)
If (current_line.Contains(names_combo_box_Array(number_combo_index).ToString)) And (loaded_file_text_box_Array(count1).Contains(non_melee) Or loaded_file_text_box_Array(count1).Contains(melee_hit)) Then
'Console.WriteLine("test " + count1.ToString + " " + RTB_Selected_File.Lines(count1).ToString)
If current_line.EndsWith(".") = True Or current_line.EndsWith("!") Then
current_line = current_line.Remove(current_line.Length - 1, 1)
End If
current_line = current_line.Remove(0, 27)
' ******************************
For count2 = 0 To names_combo_box_Array.Length - 1
' The code checks that the line of text has both combatants. This is done by ensuring that they are not the same name.
If current_line.StartsWith(display_name_Array(count2)) And display_name_Array(count2).ToString <> names_combo_box_Array(number_combo_index).ToString Then
total_damage = 0
count3 = current_line.LastIndexOf("for")
count4 = current_line.LastIndexOf("points")
total_damage = Convert.ToInt64(total_damage_Array(count2)) + Convert.ToInt64(current_line.Substring(count3 + 4, count4 - (count3 + 4)))
total_damage_Array(count2) = total_damage
' The code around here checks previous line to see if it was a critical attack
' previous_line critical_hit critical_blast critical_undead
ElseIf current_line.Contains(display_name_Array(count2)) And display_name_Array(count2).ToString <> names_combo_box_Array(number_combo_index).ToString Then
total_damage = 0
End If
Next
' ******************************
Else
'Console.WriteLine("here")
End If
Next
Console.WriteLine("end")
' ===================================================================
' adds the data to the display grid, names, damage, duration, dps etc
Data_ParseTable.Rows.Clear()
For count1 = 0 To total_damage_Array.GetUpperBound(0)
If display_name_Array(count1) <> "" Then
Data_ParseTable.Rows.Add(display_name_Array(count1), Val(total_damage_Array(count1)))
End If
Next
Else ' parse a Player's toon- this is known because there is at least one space in the name.
Console.WriteLine("Parsing a player's toon")
End If
Else ' parse from time to time
Console.WriteLine("parsing time")
End If
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
|