Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- namespace Bench
- {
- class Program
- {
- static void Main(string[] args)
- {
- var random = new Random();
- var list = Enumerable.Range(0, 1000000).Select(number => random.NextDouble() + number);
- var result = new[] { GetTime(list, YobaLinqTest),
- GetTime(list, ParsingDoubleNewTest),
- GetTime(list, ExtensionDoubleTest)
- };
- foreach(var time in result)
- {
- Console.WriteLine(time);
- }
- }
- static TimeSpan GetTime(IEnumerable<double> collection, Action<double> action)
- {
- var bench = new Stopwatch();
- bench.Start();
- foreach (var number in collection)
- {
- action(number);
- }
- bench.Stop();
- return bench.Elapsed;
- }
- static void YobaLinqTest(double value)
- {
- value.ToString().Split(new[] { ',', '.' }).Select(number => Convert.ToInt64(number));
- }
- static void ParsingDoubleNewTest(double value)
- {
- string s = Math.Abs(value).ToString();
- int index = s.IndexOf(Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
- long integer = index != -1 ? Convert.ToInt64(s.Substring(0, index)) : Convert.ToInt32(s);
- long fractional = index != -1 ? Convert.ToInt64(s.Substring(index + 1)) : 0;
- }
- static void ExtensionDoubleTest(double value)
- {
- value.IntPart();
- value.FractPart();
- }
- }
- }
- public static class DoubleExtentions
- {
- public static long IntPart(this double value)
- {
- return (long)Math.Truncate(value);
- }
- public static long FractPart(this double value)
- {
- var str = value.ToString();
- int sepIndex = str.LastIndexOf(Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
- return (sepIndex != -1) ? long.Parse(str.Substring(sepIndex + 1)) : 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement