double calc_rate (wgint bytes, double secs, int *units) { double dlrate; double bibyte = 1000.0; if (!opt.report_bps) bibyte = 1024.0; assert (secs >= 0); assert (bytes >= 0); if (secs == 0) /* If elapsed time is exactly zero, it means we're under the resolution of the timer. This can easily happen on systems that use time() for the timer. Since the interval lies between 0 and the timer's resolution, assume half the resolution. */ secs = ptimer_resolution () / 2.0; dlrate = convert_to_bits (bytes) / secs; if (dlrate < bibyte) *units = 0; else if (dlrate < (bibyte * bibyte)) *units = 1, dlrate /= bibyte; else if (dlrate < (bibyte * bibyte * bibyte)) *units = 2, dlrate /= (bibyte * bibyte); else /* Maybe someone will need this, one day. */ *units = 3, dlrate /= (bibyte * bibyte * bibyte); return dlrate; }
/* Calculate the download rate and trim it as appropriate for the speed. Appropriate means that if rate is greater than 1K/s, kilobytes are used, and if rate is greater than 1MB/s, megabytes are used. UNITS is zero for B/s, one for KB/s, two for MB/s, and three for GB/s. */ double calc_rate (wgint bytes, double msecs, int *units) { double dlrate; assert (msecs >= 0); assert (bytes >= 0); if (msecs == 0) /* If elapsed time is exactly zero, it means we're under the resolution of the timer. This can easily happen on systems that use time() for the timer. Since the interval lies between 0 and the timer's resolution, assume half the resolution. */ msecs = ptimer_resolution () / 2.0; dlrate = 1000.0 * bytes / msecs; if (dlrate < 1024.0) *units = 0; else if (dlrate < 1024.0 * 1024.0) *units = 1, dlrate /= 1024.0; else if (dlrate < 1024.0 * 1024.0 * 1024.0) *units = 2, dlrate /= (1024.0 * 1024.0); else /* Maybe someone will need this, one day. */ *units = 3, dlrate /= (1024.0 * 1024.0 * 1024.0); return dlrate; }