Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何正确测量NSInputStream数据速率_Objective C_Ios_Ios5_Nsrunloop_Nsinputstream - Fatal编程技术网

Objective c 如何正确测量NSInputStream数据速率

Objective c 如何正确测量NSInputStream数据速率,objective-c,ios,ios5,nsrunloop,nsinputstream,Objective C,Ios,Ios5,Nsrunloop,Nsinputstream,我试图测量ftp下载期间的实际传输速度,下载本身正在工作,流连接在运行循环中。在事件开始和结束时,使用CFTimeGetCurrent在NSStreamVenthasbytesAvailable中进行测量,在数据写入文件后,使用(两倍)以前的时间戳CFAbsoluteTimeGetCurrent计算经过的时间,但我得到的时间绝对不合理。在模拟器和设备上测试,有人能给我启发吗 代码: 我使用的另一种方法是使用time.h和c例程来捕获时间 另一个很好的链接 “但是我得到的时间是绝对不合理的”——

我试图测量ftp下载期间的实际传输速度,下载本身正在工作,流连接在运行循环中。在事件开始和结束时,使用CFTimeGetCurrent在NSStreamVenthasbytesAvailable中进行测量,在数据写入文件后,使用(两倍)以前的时间戳CFAbsoluteTimeGetCurrent计算经过的时间,但我得到的时间绝对不合理。在模拟器和设备上测试,有人能给我启发吗

代码:


我使用的另一种方法是使用time.h和c例程来捕获时间

另一个很好的链接

“但是我得到的时间是绝对不合理的”——你得到了什么价值观?你期待什么?为什么?我的目标是以KB/s为单位计算传输速率,所以当我除以tesread除以经过的时间和1024(得到KB)时,结果太大了,比如30000kb/s,这在我的互联网连接上是不可能的……那么“经过的时间”有什么单位呢?秒?毫秒?我尝试了第一个链接,但没有成功,c_时间似乎取决于cpu。第二个aproach给出了更为实际的值,但操作之间的时间似乎仍然很短=>计算带宽很高…如果我是正确的,在上面的方法中,新的时间是从之前的时间中减去的。这通常会导致负的差异,因为新的时间戳应始终大于旧的时间戳。您知道“downloadSpeedSave”例程如何处理负数吗?您是否尝试过“CFAbsoluteTimeGetCurrent()-previousTimestamp”?另外,更正确的方法是将“CFAbsoluteTimeGetCurrent()”返回的值存储到一个变量中,然后将其用于调用“downloadSpeedSave”和设置“previousTimestamp”!您对CFAbsoluteTimeGetCurrent()进行了两次调用,这意味着您应该得到一个用于速度计算的返回值,以及另一个设置为previousTimeStamp的返回值。您确实希望选择一个您引用的时间点。
    switch (eventCode) {
    case NSStreamEventOpenCompleted: {            
    } break;
    case NSStreamEventHasBytesAvailable: {

        if ([[self.fileStream propertyForKey:NSStreamFileCurrentOffsetKey] intValue]==0) {

            previousTimestamp = CFAbsoluteTimeGetCurrent();

        }

        NSInteger       bytesRead;
        uint8_t         buffer[32768];

        bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];


        if (bytesRead == -1) 
        {
            [self _stopReceiveWithStatus:@"Err"];
        } 
        else if (bytesRead == 0) 
        {

            [self _stopReceiveWithStatus:nil];
        } 
        else 
        {
            [self completition:bytesRead];

            NSInteger   bytesWritten;
            NSInteger   bytesWrittenSoFar;

            // Write to the file.

            bytesWrittenSoFar = 0;
            do {
                bytesWritten = [self.fileStream write:&buffer[bytesWrittenSoFar] maxLength:bytesRead - bytesWrittenSoFar];
                assert(bytesWritten != 0);
                if (bytesWritten == -1) {
                    [self _stopReceiveWithStatus:@"File err"];
                    break;
                } else {
                    bytesWrittenSoFar += bytesWritten;
                }
            } while (bytesWrittenSoFar != bytesRead);

            [self downloadSpeedSave:bytesRead :previousTimestamp-CFAbsoluteTimeGetCurrent()];


            previousTimestamp = CFAbsoluteTimeGetCurrent();