Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- namespace Test
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- Random r = new Random();
- List<string> list = new List<string>();
- for (int i = 0; i < 1000000; ++i)
- {
- string listItem = "";
- for (int j = 0; j < 4; ++j)
- {
- listItem += r.Next(10).ToString();
- }
- int index;
- if ((index = r.Next(2)) == 0)
- {
- listItem = listItem.Replace(listItem [index], (char)(r.Next(short.MaxValue - 20) + 20));
- }
- list.Add(listItem);
- }
- Stopwatch sw = new Stopwatch();
- sw.Start();
- {
- int dummy = 0;
- for (int i = 0; i < list.Count; ++i)
- {
- string str = list [i];
- try
- {
- Convert.ToInt32(str);
- ++dummy;
- } catch
- {
- --dummy;
- }
- }
- }
- sw.Stop();
- long method1 = sw.ElapsedMilliseconds;
- sw.Reset();
- sw.Start();
- {
- int dummy = 0;
- for (int i = 0; i < list.Count; ++i)
- {
- string str = list [i];
- int output;
- if (int.TryParse(str, out output))
- {
- ++dummy;
- }
- else
- {
- --dummy;
- }
- }
- }
- sw.Stop();
- long method2 = sw.ElapsedMilliseconds;
- Console.WriteLine("METHOD 1: {0}", method1);
- Console.WriteLine("METHOD 2: {0}", method2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement