objective-c 计算连接/下载速度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:59:41  来源:igfitidea点击:

Calculating Connection/Download Speed

objective-ccocoa

提问by kdbdallas

I have a client and server program (both in Obj-C) and I am transferring files between two devices using the programs.

我有一个客户端和服务器程序(都在 Obj-C 中),我正在使用这些程序在两个设备之间传输文件。

The transferring is working fine, but I would like to display to the user what transfer rate they are getting.

传输工作正常,但我想向用户显示他们获得的传输速率。

So I know the total size of the file, and how much of the file has been transferred, is there a way to figure out the transfer rate from this information, and if not, what information do I need to calculate the transfer rate?

所以我知道文件的总大小,以及传输了多少文件,有没有办法从这些信息中计算出传输率,如果没有,我需要什么信息来计算传输率?

Thanks

谢谢

回答by Peter Hosey

The na?ve way is bytes_downloaded / (now - start_time), but that becomes inaccurate if the connection speed fluctuates wildly, or if the user starts another download (perhaps in another app) halfway through your download. Both of these may happen if the user runs a torrent in the background.

天真的方法是bytes_downloaded / (now - start_time),但如果连接速度波动很大,或者用户在下载中途开始另一个下载(可能在另一个应用程序中),这就会变得不准确。如果用户在后台运行种子文件,这两种情况都可能发生。

A better way (though harder to implement) is to keep an array of periodic samples and present the average.

更好的方法(虽然更难实现)是保留一组周期性样本并呈现平均值。

Start out with an array containing 0. The array is of samples, and each sample is the number of bytes downloaded since the previous sample. Then start the download.

从一个包含 0 的数组开始。该数组由样本组成,每个样本是自上一个样本以来下载的字节数。然后开始下载。

Every half-second (you can try different intervals), measure how many bytes you've downloaded, then subtract the previous total from this new total. Add the difference as the new last element?in the array. If this grows the array beyond a certain size, lop off the first element (oldest sample). Then, present the average of all the samples.

每半秒(您可以尝试不同的时间间隔),测量您下载的字节数,然后从新的总数中减去之前的总数。添加差异作为新的最后一个元素?在数组中。如果这使数组超出特定大小,则删除第一个元素(最旧的样本)。然后,呈现所有样本的平均值。

You should keep somewhere between 2–5 seconds' worth of samples, and the interval should be somewhere between 0.5 and 1 seconds (it's a trade-off between currency and performance).

您应该保留 2 到 5 秒之间的样本,间隔应该在 0.5 到 1 秒之间(这是货币和性能之间的权衡)。

You may also want to remove the first element from the array after retrieving it if it is zero. This makes your starting reportage more accurate and helps you recover more quickly from stalls, since you're not including old zeroes in the average.

如果第一个元素为零,您可能还想在检索它后从数组中删除它。这使您的开始报道更加准确,并帮助您更快地从停顿中恢复过来,因为您没有在平均值中包含旧的零。

回答by Marc Novakowski

As soon as you start the download, capture the current system time and store it as the "start time". Then, all you need to do to calculate transfer speed at any point during the transfer is to look at the system time again and use it as the "current time" to calculate the total time spent so far:

开始下载后,立即捕获当前系统时间并将其存储为“开始时间”。然后,在传输过程中的任何一点计算传输速度所需要做的就是再次查看系统时间并将其用作“当前时间”来计算到目前为止所花费的总时间:

transfer_speed = bytes_transferred / ( current_time - start_time)

You probably want to use second or millisecond accuracy with the times, and of course can multiply the result by 8 if you want bits/second.

您可能希望对时间使用秒或毫秒精度,如果您想要位/秒,当然可以将结果乘以 8。

Since you're using Cocoa, you could use the NSDate class to get the timestamps. For example, use the following when you start the transfer:

由于您使用的是 Cocoa,您可以使用 NSDate 类来获取时间戳。例如,在开始传输时使用以下内容:

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

Then periodically check the transfer rate by using:

然后使用以下方法定期检查传输速率:

double speed = bytesTransferred / ([NSDate timeIntervalSinceReferenceDate] - start);

回答by Chirantan

You will need to know

你需要知道

  1. Bytes sent from the beginning (To calculate average rate of transfer)

  2. Bytes sent since last second (To calculate current transfer speed.)

    This can be easily done if you know bytes sent from the beginning. (New bytes sent from the beginning - Bytes sent from the beginning 1 second ago)

  3. Total file size (To calculate percentage of progress.)

    (Bytes transfered / Total number of bytes)

  1. 从开始发送的字节数(计算平均传输率)

  2. 自上一秒以来发送的字节数(计算当前传输速度。)

    如果您知道从一开始就发送的字节,这可以轻松完成。(从头开始发送的新字节 - 1秒前从头开始发送的字节)

  3. 总文件大小(计算进度百分比。)

    (传输的字节数/总字节数)

回答by Piskvor left the building

Keep track of the time that the transfer is taking. The transfer rate is simply bytes transferred/seconds elapsed, in other words bytes per second. You don't need the total size for this. (You can also show % completed, which is bytes transferred/total bytes*100)

跟踪传输所花费的时间。传输速率只是传输的字节数/经过的秒数,即每秒字节数。为此,您不需要总大小。(您还可以显示完成百分比,即传输的字节数/总字节数*100)