From fb913a6bba3f2c0c530e69a7ac0d4d577c641577 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 30 Mar 2006 19:06:40 +0000 Subject: [PATCH] Go back to using the median value for our delta estimate. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3991 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/client/DeltaCalculator.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/java/com/threerings/presents/client/DeltaCalculator.java b/src/java/com/threerings/presents/client/DeltaCalculator.java index bb82c1915..730ea4183 100644 --- a/src/java/com/threerings/presents/client/DeltaCalculator.java +++ b/src/java/com/threerings/presents/client/DeltaCalculator.java @@ -106,26 +106,37 @@ public class DeltaCalculator return 0L; } - if (true) { - // return the mean - long est = 0; - for (int ii=0; ii < _iter; ii++) { - est += _deltas[ii]; - } - return (est / _iter); + // Return a median value as our estimate, rather than an average. + // Mdb writes: + // ----------- + // I used the median because that was more likely to result in a + // sensible value. + // + // Assuming there are two kinds of packets, one that goes and comes + // back without delay and provides an accurate time value, and one + // that gets delayed somewhere on the way there or the way back and + // provides an inaccurate time value. + // + // If no packets are delayed, both algorithms should be fine. If one + // packet is delayed the median will select the middle, non-delayed + // packet, whereas the average will skew everything a bit because + // of the delayed packet. If two packets are delayed, the median + // will be more skewed than the average because it will benefit + // from the one accurate packet and if all three packets are delayed + // both algorithms will be (approximately) equally inaccurate. + // + // I believe the chances are most likely that zero or one packets + // will be delayed, so I chose the median rather than the average. + // ----------- - } else { - // return the median value + // copy the deltas array so that we don't alter things before + // all pongs have arrived + long[] deltasCopy = new long[_iter]; + System.arraycopy(_deltas, 0, deltasCopy, 0, _iter); - // copy the deltas array so that we don't alter things before - // all pongs have arrived - long[] deltasCopy = new long[_iter]; - System.arraycopy(_deltas, 0, deltasCopy, 0, _iter); - - // sort the estimates and return one from the middle - Arrays.sort(deltasCopy); - return deltasCopy[deltasCopy.length/2]; - } + // sort the estimates and return one from the middle + Arrays.sort(deltasCopy); + return deltasCopy[deltasCopy.length/2]; } /**