⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perftests.cs

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace CodeForChapter5cs
{
  class PerfTests
  {
    #region string vs stringbuilder
    public static void UseString()
    {
      string result = string.Empty;
      for (int i = 0; i < 10000; i++)
      {
        result += "string are immutable " +
          "but I still use them as if they are not";
      }
    }

    public static void UseStringBuilder()
    {
      string result = string.Empty;
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < 10000; i++)
      {
        sb.Append("string are immutable ").Append(
          "but I still use them as if they are not");
      }

      result = sb.ToString();
    }
    #endregion

    #region Collections test
    public static void UseArrayList()
    {
      ArrayList a1 = new ArrayList(100000);
      ArrayList a2 = new ArrayList(100000);

      for (int i = 0; i < 100000; i++)
      {
        a2.Add(i * i); // boxing
      }

      for (int i = 0; i < 100000; i++)
      {
        int j = (int)a2[i]; //unboxing
        a1.Add(j); //boxing
      }

    }

    public static void UseGenerics()
    {
      List<int> a1 = new List<int>(100000);
      List<int> a2 = new List<int>(100000);

      for (int i = 0; i < 100000; i++)
      {
        a2.Add(i * i);
      }

      for (int i = 0; i < 100000; i++)
      {
        int j = a2[i];
        a1.Add(j);
      }
    }

    public static void UseArray()
    {
      int[] a1 = new int[100000];
      int[] a2 = new int[100000];

      for (int i = 0; i < 100000; i++)
      {
        a2[i] = i * i;
      }

      for (int i = 0; i < 100000; i++)
      {
        int j = a2[i];
        a1[i] = j;
      }
    }

    #endregion
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -