Annette
Sun Oct 23 00:44:59 CDT 2005
Thanks Oliver - that works awesomely!
As for it being the average speed, what do you think would be the best way
to have it more accurate - reset the start time every say, 50 or 100 loops?
Cheers.
"Oliver Sturm" <oliver@sturmnet.org> wrote in message
news:xn0e8q0pl38dw2m000@msnews.microsoft.com...
> Annette Miller wrote:
>
>>In my app I am using the following code to read from the response stream
>>of a web page.
>>
>>..
>>do
>>{
>> bytesRead = receiveStream.Read(byteArray, 0, 256);
>> receiveLocal.Write(byteArray, 0, bytesRead);
>>} (while bytesRead > 0)
>>..
>>
>>where receiveStream is the Response Stream from the HttpWebRequest and
>>receiveLocal is a memory stream. Now I'm just wondering - what is the best
>>way to work out the download speed per second.
>
> Well, to extend your sample, maybe something like this:
>
> int bytesTotal = 0;
> int bytesPerSecond = 0;
> DateTime startTime = DateTime.Now;
>
> do {
> bytesRead = receiveStream.Read(byteArray, 0, 256);
> bytesTotal += bytesRead;
> receiveLocal.Write(byteArray, 0, bytesRead);
> bytesPerSecond = bytesTotal / (DateTime.Now - startTime).TotalSeconds;
> } while(bytesRead > 0);
>
> Be aware that there are several different algorithm of calculating the
> average throughput - the status dialogs for file copying or program
> installation are famous for their inaccuracy in "time left" prognoses,
> which is one example of an algorithm that's often not appropriate for the
> task.
>
> But this depends on what exactly you're going to do with the calculated
> values. The average value calculated by my sample is not wrong, but
> depending on its purpose there could be more useful ways to do the same
> (or a very similar) thing.
>
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See
http://www.sturmnet.org (try /blog as well)