title: Need to optimise counting positive and negative values
body: <p>I need to optimise code that counts pos/neg values and remove non-qualified values by time.</p>
  
  <p>I have queue of values with time-stamp attached.<br>
  I need to discard values which are 1ms old and count negative and positive values. here is pseudo code</p>
  
  <pre><code>list&lt;val&gt; l;
  v = q.dequeue();
  deleteold(l, v.time);
  l.add(v);
  negcount = l.count(i =&gt; i.value &lt; 0);
  poscount = l.count(i =&gt; i.value &gt;= 0);
  if(negcount == 10) return -1;
  if(poscount == 10) return  1;
  </code></pre>
  
  <p>I need this code in c# working with max speed. No need to stick to the List. In fact arrays separated for neg and pos values are welcome.</p>
  
  <p><strong>edit: probably unsafe arrays will be the best. any hints?</strong></p>
  
  <p><strong>EDIT: thanks for the heads up..</strong> i quickly tested array version vs list (which i already have) and the list is faster: 35 vs 16 ms for 1 mil iterations...</p>
  
  <p>Here is the code for fairness sake:</p>
  
  <pre><code>class Program
  {
      static int LEN = 10;
      static int LEN1 = 9;
  
      static void Main(string[] args)
      {
          Var[] data = GenerateData();
  
          Stopwatch sw = new Stopwatch();
  
          for (int i = 0; i &lt; 30; i++)
          {
              sw.Reset();
              ArraysMethod(data, sw);
  
              Console.Write("Array: {0:0.0000}ms     ", sw.ElapsedTicks / 10000.0);
  
              sw.Reset();
              ListMethod(data, sw);
  
              Console.WriteLine("List: {0:0.0000}ms", sw.ElapsedTicks / 10000.0);
          }
  
          Console.ReadLine();
      }
  
      private static void ArraysMethod(Var[] data, Stopwatch sw)
      {
          int signal = 0;
          int ni = 0, pi = 0;
          Var[] n = new Var[LEN];
          Var[] p = new Var[LEN];
          for (int i = 0; i &lt; LEN; i++)
          {
              n[i] = new Var();
              p[i] = new Var();
          }
  
          sw.Start();
          for (int i = 0; i &lt; DATALEN; i++)
          {
              Var v = data[i];
  
              if (v.val &lt; 0)
              {
                  int x = 0;
                  ni = 0;
                  // time is not sequential
                  for (int j = 0; j &lt; LEN; j++)
                  {
                      long diff = v.time - n[j].time;
                      if (diff &lt; 0)
                          diff = 0;
  
                      // too old
                      if (diff &gt; 10000)
                          x = j;
                      else
                          ni++;
  
                  }
  
                  n[x] = v;
  
                  if (ni &gt;= LEN1)
                      signal = -1;
  
              }
              else
              {
                  int x = 0;
                  pi = 0;
                  // time is not sequential
                  for (int j = 0; j &lt; LEN; j++)
                  {
                      long diff = v.time - p[j].time;
                      if (diff &lt; 0)
                          diff = 0;
  
                      // too old
                      if (diff &gt; 10000)
                          x = j;
                      else
                          pi++;
  
                  }
  
                  p[x] = v;
  
                  if (pi &gt;= LEN1)
                      signal = 1;
              }
          }
          sw.Stop();
      }
  
      private static void ListMethod(Var[] data, Stopwatch sw)
      {
          int signal = 0;
          List&lt;Var&gt; d = new List&lt;Var&gt;();
  
          sw.Start();
          for (int i = 0; i &lt; DATALEN; i++)
          {
              Var v = data[i];
  
  
              d.Add(new Var() { time = v.time, val = v.val &lt; 0 ? -1 : 1 });
  
              // delete expired
              for (int j = 0; j &lt; d.Count; j++)
              {
                  if (v.time - d[j].time &lt; 10000)
                      d.RemoveAt(j--);
                  else
                      break;
              }
  
              int cnt = 0;
              int k = d.Count;
              for (int j = 0; j &lt; k; j++)
              {
                  cnt += d[j].val;
              }
  
              if ((cnt &gt;= 0 ? cnt : -cnt) &gt;= LEN)
                  signal = 9;
          }
          sw.Stop();
      }
  
      static int DATALEN = 1000000;
      private static Var[] GenerateData()
      {
          Random r = new Random(DateTime.Now.Millisecond);
  
          Var[] data = new Var[DATALEN];
  
          Var prev = new Var() { val = 0, time = DateTime.Now.TimeOfDay.Ticks};
          for (int i = 0; i &lt; DATALEN; i++)
          {
              int x = r.Next(20);
              data[i] = new Var() { val = x - 10, time = prev.time + x * 1000 };
          }
  
          return data;
      }
  
      class Var
      {
          public int val;
          public long time;
      }
  
  }
  </code></pre>