title: Get upload speed from percentage uploaded
body: <p>I want to calculate the speed of a file upload to Dropbox.</p>
  
  <p>Here's the code that I'm currently using:</p>
  
  <pre><code>int numberOfCalls = 0;
  
  - (float)getUploadSpeed:(float)currentAmountUploaded {
      numberOfCalls++;
      NSLog(@"called %d times.", numberOfCalls);
      NSLog(@"speed approx. %fKB/sec.", (currentAmountUploaded / numberOfCalls)*1024);
      return (currentAmountUploaded / numberOfCalls)*1024;
  }
  
  - (void)startUploadSpeedTest:(NSNumber *)uploadCurrent {
  
      float uploadedFileSize = [uploadCurrent floatValue];
  
      NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:@selector(getUploadSpeed:)];
  
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
      [invocation setSelector:@selector(getUploadSpeed:)];
  
      [invocation setTarget:self];
      [invocation setArgument:&amp;uploadedFileSize atIndex:2];
      [invocation invoke];
  
      NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  
      [[NSTimer timerWithTimeInterval:1.0 invocation:invocation repeats:YES] retain];
  
      [runLoop run];
      [pool release];
  }
  
  - (void)restClient:(DBRestClient *)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath {
      NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:srcPath error:nil];
      float fileSize = ((float)[fileAttributes fileSize]/1024/1024);
      float uploadedFileSize = fileSize * progress;
  
      NSNumber *number = [[[NSNumber alloc] initWithFloat:uploadedFileSize] autorelease];
  
      NSThread *timerThread = [[[NSThread alloc] initWithTarget:self selector:@selector(startUploadSpeedTest:) object:number] autorelease];
      [timerThread start];
  }
  </code></pre>
  
  <p>However, the timer isn't called every second, and I'm not sure this is the best way for obtaining the speed.</p>
  
  <p>Any help appreciated.</p>