calculate sending file speed/sec by taking the average of 5 times of sent bytes [duplicate]

為{幸葍}努か 提交于 2019-11-29 18:46:38

I don't understand why you are using the long[] rate variable... If you want to calculate the transfer rate and update it each second you should store the current fileSize in a variable, and then after the sleep see the new fileSize. Then substract the previous fileSieze from the new one and you have the transfer rate for the last second (the live transfer rate). For the general transfer rate you should calculate it by taking a time-stamp when the download/upload started, and then, after each sleep calculate the rate by dividing the current fileSize with the total seconds passed so far.

Instead of calculating the average every time (which can become slow, if your rates array becomes very large), you can calculate it this way.

consider your rates to be these, for the sake of this example

long[] rates = new long[] { 5, 1, 3,4 ,2 ,5, 1};

you can calculate the average on each step like this:

double currentAverage = 0;
for (int i = 0; i < rates.Length; i++)
{
   long currentRate = rates[i];
   int iteration = i + 1;
   currentAverage = (currentAverage * i + currentRate) / iteration;
}

Then just wait a second for each rate sample.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!