c# - WebClient DownloadDataAsync current download speed -


i'm trying current download speed of webclient downloading file, when use formula i'm pretty sure should work out:

stopwatch.stop(); double mselapsed = stopwatch.elapsed.totalmilliseconds; int bytesdownloaded = (int)e.bytesreceived - lastbytesdownloaded; double downloadspeed = (double)bytesdownloaded / (mselapsed / 1000); lastbytesdownloaded = (int)e.bytesreceived; stopwatch.restart(); 

where stopwatch stopwatch i've started started file download, lastbytesdownloaded class variable, , inside downloadprogresschanged event, download speed varies wildly off course is.

for example if downloading file @ 500kb/s, rapidly jump (for example) 10kb/s 50mb/s randomly.

i can accurate average download time making couple edits that:

double selapsed = stopwatch.elapsed.totalseconds; int bytesdownloaded = (int)e.bytesreceived; double downloadspeed = bytesdownloaded / selapsed; 

but isn't want. how can more stable reading current download speed?

you need smooth data on longer period of time. example, don't report current download speed on basis of last measurement; use (perhaps weighted) moving average instead.

dead simple example:

var measurements = 0, maxdatapoints = 5; var datapoints = new double[maxdatapoints]; 

and then:

stopwatch.stop(); double mselapsed = stopwatch.elapsed.totalmilliseconds; int bytesdownloaded = (int)e.bytesreceived - lastbytesdownloaded; lastbytesdownloaded = (int)e.bytesreceived; double datapoint = (double)bytesdownloaded / (mselapsed / 1000); datapoints[measurements++ % maxdatapoints] = datapoint;  double downloadspeed = datapoints.average(); stopwatch.restart(); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -