title: chars per second, KBps, Kilo[bits/bytes] per second, (computer network), how compute right
body: <p>Sorry for maybe stupid question, but how is really Kbps/... (kilobits per second, and kilobytes per second) is computed?
  I have such computing now:</p>
  
  <pre><code>DWORD ibytesin=0,ibytes_sttime=0,ibytes_st=0,ibps=0,ilastlen;
  DWORD obytesin=0,obytes_sttime=0,obytes_st=0,obps=0,olastlen;
  </code></pre>
  
  <p>ibytesin - total bytes in for all time;</p>
  
  <p>ibytes_sttime - time when start ibytes_st was assigned;</p>
  
  <p>ibytes_st - bytes count at time of ibytes_sttime;</p>
  
  <p>ibps - kbps/bps/...;</p>
  
  <p>ilastlen - to be more accurate and because my protocol uses request packets, i do not want to take last in length;</p>
  
  <p>The same rules for out traffic (o*).</p>
  
  <p>First harvest for example in bytes:</p>
  
  <pre><code>len = recv(ConnectSocket, (char*)p, readleft, 0);
  if(len&gt;0) {
      ibytesin+=len;
      ilastlen=len;
  }
  </code></pre>
  
  <p>Same for out.</p>
  
  <p>Then later in some fast frequently executed place, in stats thread for example:</p>
  
  <pre><code>if ((GetTickCount() - obytes_sttime) &gt;= 1000) // update once per second
  {
      obps = (obytesin-obytes_st-olastlen) / 1024 * 8;
      obytes_sttime = GetTickCount();
      obytes_st = obytesin;
      olastlen=0;
  }
  
  if ((GetTickCount() - ibytes_sttime) &gt;= 1000) // update once per second
  {
      ibps = (ibytesin-ibytes_st-ilastlen) / 1024* 8; // get kilobytes*8 == Kbps ?
      ibytes_sttime = GetTickCount();
      ibytes_st = ibytesin;
      ilastlen=0;
  }
  
  sprintf(str, "In/Out %3d / %-3d Kbps/сек", ibps,obps);
  </code></pre>
  
  <p>I have errorenous speed when i try to increase updates bps shows. For example i want to recalculate it at every 100 msecs, not 1 sec, so as i can imagine i need to divide ibps not by 1024, but by 102 (as 1000/10=100, so 1024/10=102.4), but the speed rate is not computed rightly 100%, it is increased, or i do mistake in my first tryings. 
  How to do right?</p>