daily pastebin goal
48%
SHARE
TWEET

test 'StringJoin" versus 'String.Join' in .NET 4.7

a guest Nov 11th, 2017 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. static class Program
  5. {
  6.     static string[] rnds = new string[10000];
  7.  
  8.     static Program()
  9.     {
  10.         Stopwatch.GetTimestamp();
  11.         Stopwatch.GetTimestamp();
  12.         Stopwatch.GetTimestamp();
  13.         Stopwatch.GetTimestamp();
  14.  
  15.         rnd = new Random((int)(Stopwatch.GetTimestamp() >> 5));
  16.  
  17.         for (int i = 0; i < rnds.Length; i++)
  18.             rnds[i] = new string((Char)('!' + rnd.Next('~' - '!')), rnd.Next(7) + 1);
  19.  
  20.         // JIT warm-up
  21.         var rgs = GetStrings();
  22.         orderingA(rgs);
  23.         orderingB(rgs);
  24.         // reset timing
  25.         t1 = t2 = 0;
  26.     }
  27.  
  28.     readonly static Random rnd;
  29.     static long t1, t2;
  30.  
  31.     static void Main(string[] args)
  32.     {
  33.         int i = 10000000;
  34.         do
  35.         {
  36.             if ((i & 1) == 0)
  37.                 orderingA(GetStrings());
  38.             else
  39.                 orderingB(GetStrings());
  40.         }
  41.         while (--i > 0);
  42.  
  43.         Console.WriteLine("'StringJoin' {0:#,###}", t1);
  44.         Console.WriteLine("String.Join  {0:#,###}", t2);
  45.         Console.WriteLine("{0:N2}", (t2 - t1) * 100.0 / Math.Min(t1, t2));
  46.     }
  47.  
  48.     static string[] GetStrings()
  49.     {
  50.         var rgs = new String[rnd.Next(100) + 2];
  51.         for (int i = 0; i < rgs.Length; i++)
  52.             rgs[i] = rnds[rnd.Next(rnds.Length)];
  53.         return rgs;
  54.     }
  55.  
  56.     static void orderingA(String[] rgs)
  57.     {
  58.         test1(rgs);
  59.         test2(rgs);
  60.     }
  61.     static void orderingB(String[] rgs)
  62.     {
  63.         test2(rgs);
  64.         test1(rgs);
  65.     }
  66.  
  67.     static void test1(String[] rgs)
  68.     {
  69.         var t = Stopwatch.GetTimestamp();
  70.         alib.Enumerable.enum_ext.StringJoin(rgs);
  71.         t1 += Stopwatch.GetTimestamp() - t;
  72.     }
  73.     static void test2(String[] rgs)
  74.     {
  75.         var t = Stopwatch.GetTimestamp();
  76.         String.Join(String.Empty, rgs);
  77.         t2 += Stopwatch.GetTimestamp() - t;
  78.     }
  79. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top