【问题标题】:What's the fastest way to convert a long list of int[16] to 16 long int[] in C#在 C# 中将一长串 int[16] 转换为 16 long int[] 的最快方法是什么
【发布时间】:2012-10-29 15:48:42
【问题描述】:

我有一个很长的(10000 多个条目)结构列表,每个结构都包含一个 int[16]。我想转置数据以创建 16 个 int[] 数组,这些数组长度为 10000+ 个条目。本质上,我想转置数据。有没有比遍历列表和创建新条目更快的方法?

【问题讨论】:

  • 这个链接可能对stackoverflow.com/questions/4801990/…有帮助
  • 如果你可以发布你已经拥有的代码,人们可以建议优化,否则从你上面的描述中听起来好像没有更快的方法。需要创建数组并复制数据,没有办法做最少的事情。
  • 更快是什么意思?更少的代码,更高的性能?
  • 您是否可能不需要数组的物理副本,而只是一种更简单的方法来使用索引访问 int 值?

标签: c# list transpose


【解决方案1】:

所以你只想要一个 int[16] 的数组?

也许 linq 可以做到这一点

var newArray = fooArray.Select(foo => foo.IntVal).ToArray();

【讨论】:

    【解决方案2】:

    你应该这样做:

    struct Widget
    {
      public int[] Values = new int[16]; 
    }
    
    public int[][] TransposeWidgets( List<Widget> widgets )
    {
      int cols;
      int[][] result = new int[][];
    
      for ( int row = 0 ; i < 16 ; ++row )
      {
        results[row] = new int[ widgets.Count ];
        for ( int col = 0 ; col < widgets.Count ; ++col )
        {
          result[row][col] = widgets[col].Values[row];
        }
      }
    
    }
    

    虽然对大列表进行一次迭代可能会更快。如果它真的很大,遍历该列表可能会导致分页,这会减慢速度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 2016-05-16
      • 2013-02-17
      相关资源
      最近更新 更多