C # answer exercises: Performance # 3 - Fill in a matrix array Int- - Need for Speed [the highest degree of difficulty: Level 2] - view the C # programming classic exam, you C # 1000 to challenge the basis of exercises, etc.

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45444821/article/details/102750610

Performance # 3 - Fill in a matrix array Int- - Need for Speed ​​[the highest degree of difficulty: Level 2]:

Answer 1:

using System;
using System.Linq;

public static class Kata
{
  private static int[] cache = Enumerable.Range(0,4000000).ToArray();
  public static int[] Performance() => cache;
}

Answer 2:

using System;
using System.Linq;

public static class Kata
{
  static Kata()
  {
    array = new int[4000000];
    for(int i=0;i<4000000;i++)
    {
      array[i] = i;
    }
  }
  
  private static int[] array = null;
  
  public static int[] Performance()
  {     
    return array;
  }
}

Answer 3:

using System;
using System.Linq;

public static class Kata
{
  static int[] result = null;

  public static int[] Performance()
  { 
    if(result == null)
    {
      result = new int[4000000];
      
      for(var i = 0; i < result.Length; i++)
      {
        result[i] = i;
      }
    }
      
    return result;
  }
}

Answer 4:

using System;
using System.Linq;

public static class Kata
{
  private static int[] perfCache = null;
  public static int[] Performance()
  {
    if (perfCache == null)
    {
      perfCache = Enumerable.Range(0, 4000000).ToArray();
    }

    return perfCache;
  }
}

Answer 5:

using System.Threading.Tasks;

public static class Kata
{
    private static int[] Solution = null;

    public static int[] Performance()
    {
        if (Solution == null)
        {
            Solution = new int[4000000];
            Parallel.For(0, 4000000, i => Solution[i] = i);
        }

        return Solution;
    }
}

A6:

using System;
using System.Linq;

public static class Kata
{
  static int count = 0;
  static int[] array = new int[4000000];
  public static int[] Performance()
  { 
    if(count < 1)
    {
      int i = 0;
      do
      {
          array[i] = i++;
      } while (i < array.Length);
      count++;
    }
    return array;
  }
}

A7:

using System;
using System.Linq;

public static class Kata
{
    private static readonly int[] values = GenerateValues();

    private static int[] GenerateValues()
    {
        int[] result = new int[4000000];
        for (int i = 0; i < 4000000; i++)
        {
            result[i] = i;
        }
        return result;
    }

    public static int[] Performance() => values;
}

A8:

using System;
using System.Linq;

public static class Kata
{
  private static int[] cache; 
  public static int[] Performance()
  {
    if(cache == null) {
      cache = new int[4000000];
      for(var i = 0; i < 4000000; i++)
        cache[i] = i;
    }
    return cache;
  }
}

A9:

using System;
using System.Linq;

public static class Kata
{
private static int[] cache = null;
  public static int[] Performance()
  {
    
   if(cache != null)
    {
      return cache;
    }
    
    cache = new int[4000000];
    for (int i = 0; i < cache.Length; i++)
    {
      cache[i] = i;
    }
    
    return cache;
  }
}

Answers 10:

using System;
using System.Linq;

public static class Kata
{
  private static int[] a;
  
  static Kata() {
    a = new int[4000000];
    for (int i = 0; i < 4000000; i++) {
      a[i] = i;
    }
  }

  public static int[] Performance()
  {
    return a;
  }
}




Guess you like

Origin blog.csdn.net/weixin_45444821/article/details/102750610