how to optimize this code for speed 
Author Message
 how to optimize this code for speed

In the code given below has too many conversion, string
additions etc ..what i want is just a plain string after
the XSL transformation, can some body tell me how to
optimize this code more for speed ?

XmlDocument d = new XmlDocument ();
d.Load ("My.xml");
XslTransform xt = new XslTransform ();
xt.Load ("My.xsl");
MemoryStream ms = new MemoryStream ();
StreamWriter sw = new StreamWriter ( ms);
xt.Transform  (d,null, sw );
StringBuilder  s  = new StringBuilder (1024) ;
byte []b =  ms.GetBuffer ();

foreach (byte byt in b)
{
 s = s.Append ( Convert.ToChar ( byt ).ToString ());

Quote:
}

div_id.InnerHtml = s.ToString ();
sw.Close ();


Tue, 09 Dec 2003 07:28:28 GMT  
 how to optimize this code for speed
Is there a reason not to use the Response Output Stream?  Something like
the following:

  XmlDocument doc = new XmlDocument();
  doc.Load (Server.MapPath("Story.xml"));

  XslTransform xslt = new XslTransform();
  xslt.Load (Server.MapPath("Story.xsl"));

  xslt.Transform  (doc, null, Response.OutputStream );



Quote:
> In the code given below has too many conversion, string
> additions etc ..what i want is just a plain string after
> the XSL transformation, can some body tell me how to
> optimize this code more for speed ?

> XmlDocument d = new XmlDocument ();
> d.Load ("My.xml");
> XslTransform xt = new XslTransform ();
> xt.Load ("My.xsl");
> MemoryStream ms = new MemoryStream ();
> StreamWriter sw = new StreamWriter ( ms);
> xt.Transform  (d,null, sw );
> StringBuilder  s  = new StringBuilder (1024) ;
> byte []b =  ms.GetBuffer ();

> foreach (byte byt in b)
> {
>  s = s.Append ( Convert.ToChar ( byt ).ToString ());

> }

> div_id.InnerHtml = s.ToString ();
> sw.Close ();

Tim


Tue, 09 Dec 2003 12:56:39 GMT  
 how to optimize this code for speed
No, I want to put the stream into div_id.innerhtml, and my
questions is :

Is there a short cut way to get the string other than
first get the StreamWriter, made up of memoeyStream, then
get the Byte array of memoryStream, then cast each byte
back to string ..and add all this string together ..
does't look right way to do things .. lot of
overhead ..can i get a simple string in a string type,
with out all this conversions ..

Quote:
>-----Original Message-----
>Is there a reason not to use the Response Output Stream?  
Something like
>the following:

>  XmlDocument doc = new XmlDocument();
>  doc.Load (Server.MapPath("Story.xml"));

>  XslTransform xslt = new XslTransform();
>  xslt.Load (Server.MapPath("Story.xsl"));

>  xslt.Transform  (doc, null, Response.OutputStream );



>> In the code given below has too many conversion, string
>> additions etc ..what i want is just a plain string
after
>> the XSL transformation, can some body tell me how to
>> optimize this code more for speed ?

>> XmlDocument d = new XmlDocument ();
>> d.Load ("My.xml");
>> XslTransform xt = new XslTransform ();
>> xt.Load ("My.xsl");
>> MemoryStream ms = new MemoryStream ();
>> StreamWriter sw = new StreamWriter ( ms);
>> xt.Transform  (d,null, sw );
>> StringBuilder  s  = new StringBuilder (1024) ;
>> byte []b =  ms.GetBuffer ();

>> foreach (byte byt in b)
>> {
>>  s = s.Append ( Convert.ToChar ( byt ).ToString ());

>> }

>> div_id.InnerHtml = s.ToString ();
>> sw.Close ();

>Tim
>.



Wed, 10 Dec 2003 03:52:59 GMT  
 how to optimize this code for speed
Once you have a memory stream, you can simply convert this to a string with
the follow two lines of code:

    StreamReader sr = new StreamReader(ms);
    string s = sr.ReadToEnd();

or combine them into one less readable line:

    string s = (new StreamReader(ms)).ReadToEnd();

Phil Wigglesworth
Mythlandia Inc


Quote:
> No, I want to put the stream into div_id.innerhtml, and my
> questions is :

> Is there a short cut way to get the string other than
> first get the StreamWriter, made up of memoeyStream, then
> get the Byte array of memoryStream, then cast each byte
> back to string ..and add all this string together ..
> does't look right way to do things .. lot of
> overhead ..can i get a simple string in a string type,
> with out all this conversions ..

> >-----Original Message-----
> >Is there a reason not to use the Response Output Stream?
> Something like
> >the following:

> >  XmlDocument doc = new XmlDocument();
> >  doc.Load (Server.MapPath("Story.xml"));

> >  XslTransform xslt = new XslTransform();
> >  xslt.Load (Server.MapPath("Story.xsl"));

> >  xslt.Transform  (doc, null, Response.OutputStream );



> >> In the code given below has too many conversion, string
> >> additions etc ..what i want is just a plain string
> after
> >> the XSL transformation, can some body tell me how to
> >> optimize this code more for speed ?

> >> XmlDocument d = new XmlDocument ();
> >> d.Load ("My.xml");
> >> XslTransform xt = new XslTransform ();
> >> xt.Load ("My.xsl");
> >> MemoryStream ms = new MemoryStream ();
> >> StreamWriter sw = new StreamWriter ( ms);
> >> xt.Transform  (d,null, sw );
> >> StringBuilder  s  = new StringBuilder (1024) ;
> >> byte []b =  ms.GetBuffer ();

> >> foreach (byte byt in b)
> >> {
> >>  s = s.Append ( Convert.ToChar ( byt ).ToString ());

> >> }

> >> div_id.InnerHtml = s.ToString ();
> >> sw.Close ();

> >Tim
> >.



Thu, 11 Dec 2003 13:21:17 GMT  
 how to optimize this code for speed
There are caveat to this code

Quote:
>    StreamReader sr = new StreamReader(ms);
>    string s = sr.ReadToEnd();

    StreamReader sr = new StreamReader(ms);
    //Must do this -
    sr.BaseStream.Seek (0, SeekOrigin.Begin  );  
    string s = sr.ReadToEnd();

here is the complete an example :

static void Main(string[] args)
{
MemoryStream ms = new MemoryStream(1024);
StreamWriter sw = new StreamWriter ( ms );
//Write to StreamWriter
sw.WriteLine ( Console.ReadLine ());
sw.Flush(); //MUST DO THIS to write it to underlying stream
StreamReader sr = new StreamReader  ( ms  );
//Thou shall seek to begining
sr.BaseStream.Seek (0, SeekOrigin.Begin  );
Console.WriteLine ( sr.ReadToEnd () );
sw.Close ();
ms.Close ();
Console.WriteLine ( "Press enter to exit ... ");
Console.ReadLine ();

Quote:
}

HTH

Rafat Sarosh

Quote:
>-----Original Message-----
>Once you have a memory stream, you can simply convert

this to a string with
Quote:
>the follow two lines of code:

>    StreamReader sr = new StreamReader(ms);
>    string s = sr.ReadToEnd();

>or combine them into one less readable line:

>    string s = (new StreamReader(ms)).ReadToEnd();

>Phil Wigglesworth
>Mythlandia Inc



>> No, I want to put the stream into div_id.innerhtml, and
my
>> questions is :

>> Is there a short cut way to get the string other than
>> first get the StreamWriter, made up of memoeyStream,
then
>> get the Byte array of memoryStream, then cast each byte
>> back to string ..and add all this string together ..
>> does't look right way to do things .. lot of
>> overhead ..can i get a simple string in a string type,
>> with out all this conversions ..

>> >-----Original Message-----
>> >Is there a reason not to use the Response Output
Stream?
>> Something like
>> >the following:

>> >  XmlDocument doc = new XmlDocument();
>> >  doc.Load (Server.MapPath("Story.xml"));

>> >  XslTransform xslt = new XslTransform();
>> >  xslt.Load (Server.MapPath("Story.xsl"));

>> >  xslt.Transform  (doc, null, Response.OutputStream );

>> >In article <186501c0faa9$e01407c0


- Show quoted text -

Quote:

>> >> In the code given below has too many conversion,
string
>> >> additions etc ..what i want is just a plain string
>> after
>> >> the XSL transformation, can some body tell me how to
>> >> optimize this code more for speed ?

>> >> XmlDocument d = new XmlDocument ();
>> >> d.Load ("My.xml");
>> >> XslTransform xt = new XslTransform ();
>> >> xt.Load ("My.xsl");
>> >> MemoryStream ms = new MemoryStream ();
>> >> StreamWriter sw = new StreamWriter ( ms);
>> >> xt.Transform  (d,null, sw );
>> >> StringBuilder  s  = new StringBuilder (1024) ;
>> >> byte []b =  ms.GetBuffer ();

>> >> foreach (byte byt in b)
>> >> {
>> >>  s = s.Append ( Convert.ToChar ( byt ).ToString ());

>> >> }

>> >> div_id.InnerHtml = s.ToString ();
>> >> sw.Close ();

>> >Tim
>> >.

>.



Sat, 13 Dec 2003 01:54:11 GMT  
 how to optimize this code for speed
Totally agree.
The MemoryStream must be at the start - can also use ms.Seek (0,
SeekOrigin.Begin )  on line before

ms.Seek (0, SeekOrigin.Begin );
string s = (new StreamReader(ms)).ReadToEnd();

The key is to remember that the read & write position is the same marker -
so you need to move it back to re-read items writen.

Phil Wigglesworth
Mythlandia Inc


Quote:
> There are caveat to this code

> >    StreamReader sr = new StreamReader(ms);
> >    string s = sr.ReadToEnd();

>     StreamReader sr = new StreamReader(ms);
>     //Must do this -
>     sr.BaseStream.Seek (0, SeekOrigin.Begin  );
>     string s = sr.ReadToEnd();

> here is the complete an example :

> static void Main(string[] args)
> {
> MemoryStream ms = new MemoryStream(1024);
> StreamWriter sw = new StreamWriter ( ms );
> //Write to StreamWriter
> sw.WriteLine ( Console.ReadLine ());
> sw.Flush(); //MUST DO THIS to write it to underlying stream
> StreamReader sr = new StreamReader  ( ms  );
> //Thou shall seek to begining
> sr.BaseStream.Seek (0, SeekOrigin.Begin  );
> Console.WriteLine ( sr.ReadToEnd () );
> sw.Close ();
> ms.Close ();
> Console.WriteLine ( "Press enter to exit ... ");
> Console.ReadLine ();

> }

> HTH

> Rafat Sarosh

> >-----Original Message-----
> >Once you have a memory stream, you can simply convert
> this to a string with
> >the follow two lines of code:

> >    StreamReader sr = new StreamReader(ms);
> >    string s = sr.ReadToEnd();

> >or combine them into one less readable line:

> >    string s = (new StreamReader(ms)).ReadToEnd();

> >Phil Wigglesworth
> >Mythlandia Inc



> >> No, I want to put the stream into div_id.innerhtml, and
> my
> >> questions is :

> >> Is there a short cut way to get the string other than
> >> first get the StreamWriter, made up of memoeyStream,
> then
> >> get the Byte array of memoryStream, then cast each byte
> >> back to string ..and add all this string together ..
> >> does't look right way to do things .. lot of
> >> overhead ..can i get a simple string in a string type,
> >> with out all this conversions ..

> >> >-----Original Message-----
> >> >Is there a reason not to use the Response Output
> Stream?
> >> Something like
> >> >the following:

> >> >  XmlDocument doc = new XmlDocument();
> >> >  doc.Load (Server.MapPath("Story.xml"));

> >> >  XslTransform xslt = new XslTransform();
> >> >  xslt.Load (Server.MapPath("Story.xsl"));

> >> >  xslt.Transform  (doc, null, Response.OutputStream );

> >> >In article <186501c0faa9$e01407c0


> >> >> In the code given below has too many conversion,
> string
> >> >> additions etc ..what i want is just a plain string
> >> after
> >> >> the XSL transformation, can some body tell me how to
> >> >> optimize this code more for speed ?

> >> >> XmlDocument d = new XmlDocument ();
> >> >> d.Load ("My.xml");
> >> >> XslTransform xt = new XslTransform ();
> >> >> xt.Load ("My.xsl");
> >> >> MemoryStream ms = new MemoryStream ();
> >> >> StreamWriter sw = new StreamWriter ( ms);
> >> >> xt.Transform  (d,null, sw );
> >> >> StringBuilder  s  = new StringBuilder (1024) ;
> >> >> byte []b =  ms.GetBuffer ();

> >> >> foreach (byte byt in b)
> >> >> {
> >> >>  s = s.Append ( Convert.ToChar ( byt ).ToString ());

> >> >> }

> >> >> div_id.InnerHtml = s.ToString ();
> >> >> sw.Close ();

> >> >Tim
> >> >.

> >.



Sat, 13 Dec 2003 15:05:41 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Bad Address from Speed Optimized Code

2. How to optimize execution speed of code ??

3. How to speed optimize c/c++ code

4. Problems with for speed optimized code

5. Problems with for speed optimized code

6. Optimizing random number generator (was Re: Optimizing code for tests)

7. Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

8. Speed/safety optimizing recomendation.

9. Problem with Optimize for speed in VC++ 5.0

10. Optimizing C compiler(speed)

11. Optimizing speed using inline assembly fails - Why?

12. BUG: Optimize for Speed in Release builds

 

 
Powered by phpBB® Forum Software