aewarnick Posted March 14, 2003 Posted March 14, 2003 (edited) I was told that ReadToEnd() was slower than if you put the whole thing into a byte[] and went from there. I think I am having problems, because my method is so much slower than ReadToEnd() is: public static object Read(ref string path) { string g=""; if(File.Exists(path)) { Stream F=new FileStream(path, FileMode.Open, FileAccess.Read); byte[]b=new Byte[F.Length]; F.Read(b, 0, Convert.ToInt32(F.Length)); for(int i=0; i< b.Length; i++) { if(b[i]==0) break; g+=(char)b[i]; } //StreamReader s=new StreamReader(path); //string g= s.ReadToEnd(); //s.Close(); MessageBox.Show(g); F.Close(); } return g; } Why is mine so slow? Edited March 14, 2003 by divil Quote C#
*Gurus* divil Posted March 14, 2003 *Gurus* Posted March 14, 2003 You are concatenating a string each time, which means completely recreating it for every byte in your file. To instantly convert a byte array in to a string, look at System.Text.Encoding.ASCII.GetString(). Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted March 14, 2003 Author Posted March 14, 2003 Do I have to write that whole thing all the time? Why can't I put in up at the top with all the other using's? Quote C#
*Experts* Nerseus Posted March 14, 2003 *Experts* Posted March 14, 2003 Yes, you can put "using System.Text.Encoding" at the top and call "ASCII.GetString()". -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
aewarnick Posted March 14, 2003 Author Posted March 14, 2003 Actually only using System.Text; works. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.