Advertisement
Fiolek

BigInteger.ToString is slow

Jul 5th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Numerics;
  4.  
  5. namespace Tests
  6. {
  7.     class Program
  8.     {
  9.         private static BigInteger silnia(int i)
  10.         {
  11.             BigInteger result = 1;
  12.             for (int x = 1; x <= i; x++)
  13.             {
  14.                 result *= x;
  15.             }
  16.             return result;
  17.         }
  18.  
  19.         public static void Main(string[] args)
  20.         {
  21.             Console.Write("Wprowadź liczbę, której silnię pragniesz policzyć: ");
  22.             int n = int.Parse(Console.ReadLine());
  23.             Stopwatch watch = new Stopwatch();
  24.  
  25.             watch.Start();
  26.             BigInteger resultF = silnia(n);
  27.             TimeSpan factTime = watch.Elapsed;
  28.  
  29.             watch.Restart();
  30.             string resultAsString = resultF.ToString();
  31.             TimeSpan convTime = watch.Elapsed;
  32.  
  33.             Console.WriteLine("Wynik: {0}", resultAsString);
  34.             Console.WriteLine("Czas obliczeń: {0} sekund", factTime.TotalSeconds);
  35.             Console.WriteLine("Czas konwersji: {0} sekund", convTime.TotalSeconds);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement