What's new

ASCII Search Code Slow

  • Thread starter mojobojo
  • Start date
  • Views 360
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
I made this code to search for an ASCII String bit it is pretty slow. Is there a faster way to do this. Im thinking my code isnt wrong but needs some modifications. Im still trying modifications to this code.

Code:
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileStream[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] fs = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileStream[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](fileName, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Open);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] br = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](fs);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] found = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < fs.Length; i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]   br.BaseStream.Position = i;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ASCII.GetString(br.ReadBytes(toSearch.Length));[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (toSearch == str)[/SIZE]
[SIZE=2]   {[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]       MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Found at: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + i.ToString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"X2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]

[SIZE=2]       found = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]   }[/SIZE]
[SIZE=2]}[/SIZE]

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (found == [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]    MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Not Found"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]

[SIZE=2]br.Close();[/SIZE]
[SIZE=2]fs.Close();[/SIZE]
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
mojobojo said:
I made this code to search for an ASCII String bit it is pretty slow. Is there a faster way to do this. Im thinking my code isnt wrong but needs some modifications. Im still trying modifications to this code.

Code:
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileStream[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] fs = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileStream[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](fileName, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]FileMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Open);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] br = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](fs);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] found = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < fs.Length; i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]   br.BaseStream.Position = i;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ASCII.GetString(br.ReadBytes(toSearch.Length));[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (toSearch == str)[/SIZE]
[SIZE=2]   {[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]       MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Found at: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + i.ToString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"X2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]

[SIZE=2]       found = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]   }[/SIZE]
[SIZE=2]}[/SIZE]

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (found == [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]    MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Not Found"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]

[SIZE=2]br.Close();[/SIZE]
[SIZE=2]fs.Close();[/SIZE]

try different methods of coarse.

Try #1

Search for the first letter of the word. If you come upon that letter, see if the letter works into the entire word. Then there you go you found it.

Try #2

Try using a foreach statement(may help may not)

Try #3

Google.com or Microsoft.com


Sorry thats all i got as of right now.
 
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
I will try method 1 and method 2. As for method 3 Ive been searching for a while and came up with nothing. I gonna keep trying though.
 
Top Bottom
Login
Register