开发者

C# convert an ArrayList of type ints into a Point[]

开发者 https://www.devze.com 2023-03-28 02:17 出处:网络
First of all I am running this on a Mobile Device using CE6.5.I have an ArrayList of data that are ints and for graphing purposes I want to convert in order the number it is into the list and the int

First of all I am running this on a Mobile Device using CE6.5. I have an ArrayList of data that are ints and for graphing purposes I want to convert in order the number it is into the list and the int value into the x and y values for a Point. Put all those points into a Point Array then use bufferedGraphics.DrawLines to draw that onto the form. I have a way of doing that that seems to work pretty fast but not sure if this is the best way. Any suggestions or improvements to this code?

Oh yeah dataList is usually about 450 or more depending on screen size and rotation.

public Point[] toPointArray(int w, int h) {
      Point[] p;
      int val;

      p = new Point[dataList.Count];
      for (int i = 0; i < dataList.Count; i++) {
          val = (int)dataList[i];
          if (i < p.Length)
               p[i] = new Point(i, h - (val * h) / range + (min * h) / range);
      }
      return p;
}

Some of my data coming is updating the dataList at 256 times per sec, so concerns of overwriting are there but so far this seems to work even at these speeds.

Here are some speed values on how fast this is currently, time is in secs:

Time to complete 0.000588
Time to complete 0.0005886154
Time to complete 0.0005846154
Time to complete 0.0005870769开发者_StackOverflow社区
Time to complete 0.0005830769
Time to complete 0.0005806154
Time to complete 0.0005981539
Time to complete 0.0007206154
Time to complete 0.0005836923
Time to complete 0.001039077

After taking suggestions from the answers below here is what my code looks like now and I am getting 0.00047 secs in execution time on the average. dataList is a global that is now a List instead of ArrayList, the (int) cast was taking up a quarter of the original processing time.

   List<int> dataList = new List<int>();

   public Point[] toPointArray(int w, int h) {
        Point[] p = new Point[dataList.Count];
        for (int i = 0; i < dataList.Count; i++) {
            p[i] = new Point(i, h - (dataList[i] * h) / range + (min * h) / range);
        }

        return p;
    }


EDIT - after comment about the index and Dictionary:

Point[] p = (from i in Enumerable.Range(0, dataList.Count) 
             select new Point(
                        i, 
                        h - (((int)dataList[i]) * h) / range + (min * h) / range))
            .OrderBy ((pp) => pp.X)
            .ToArray();

this works with the ArrayList etc.


1) Declare the variables at the same place you use them. No need to declare Point[] a separately (it's created right after that), and no reason to declare int val separately (it's not used outside the loop and there are no performance penalties for declaring it where you use it).
2) You know that i is < p.Length (assuming single-threaded application), remove the check.
3) Use LINQ
4) Use Microsoft's suggested naming standards (ProperCase for methods)
5) Use better variable names
6) Use better method names - this method isn't converting it to an array (or at least, that's not the main purpose of the method)
7) Use a typed datastore. We can't see what dataList is, but you shouldn't have to convert it to an int (unless it's a long that you know is an int, or somesuch)

If you can use LINQ, I haven't looked over Yahia's solution but it should work. If you can't/don't want to use LINQ, try:

public Point[] ToPointGrid(int width, int height)
{
  Point[] points = new Point[dataList.Count];
  for(int index = 0; index < dataList.Count; ++index)
  {
    points[index] = new Point(index, height - (dataList[index] * height) / range + (minimum * height) / range);
  }
  return points;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号