Advertisement
Guest User

Untitled

a guest
Jan 10th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Diagnostics;
  9.  
  10. namespace Zadanie_2
  11. {
  12.     class Program
  13.     {
  14.         static Random los = new Random();
  15.         static Stopwatch Stoper = new Stopwatch();
  16.  
  17.         #region metody
  18.         static void GenerujLiczbyIZapiszTxt(int Ile,string plik)
  19.         {
  20.             FileStream fs = new FileStream(plik, FileMode.Create, FileAccess.ReadWrite);
  21.             StreamWriter sw = new StreamWriter(fs);
  22.             for (int i = 0; i < Ile; i++)
  23.                     sw.WriteLine (Convert.ToString(los.Next(int.MinValue, int.MaxValue)));
  24.             sw.Close();
  25.             fs.Close();
  26.         }
  27.         static void GenerujLiczbyIZapiszBinary(int Ile,string plik)
  28.         {
  29.             FileStream fs = new FileStream(plik, FileMode.Create, FileAccess.ReadWrite);
  30.             BinaryWriter bw = new BinaryWriter(fs);
  31.  
  32.             for (int i = 0; i < Ile; i++)
  33.                 bw.Write(los.Next(int.MinValue, int.MaxValue));
  34.             bw.Close();
  35.             fs.Close();
  36.         }
  37.                      
  38.         static long PobierzCzasZapisuTxt(int input, string plik)
  39.         {
  40.             Stoper.Start();
  41.             GenerujLiczbyIZapiszTxt(input, plik);
  42.             Stoper.Stop();
  43.             long res = Stoper.ElapsedMilliseconds;
  44.             Stoper.Reset();
  45.             return res;
  46.         }
  47.         static long PobierzCzasZapisuBinary(int input,string plik)
  48.         {
  49.             Stoper.Start();
  50.             GenerujLiczbyIZapiszBinary(input, plik);
  51.             Stoper.Stop();
  52.             long res = Stoper.ElapsedMilliseconds;
  53.             Stoper.Reset();
  54.             return res;
  55.         }
  56.  
  57.         static void PoliczLiczbyParzysteTxt(string plik)
  58.         {
  59.             FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
  60.             StreamReader sr = new StreamReader(fs);
  61.             int ile = 0;
  62.             while (!sr.EndOfStream)
  63.             {
  64.                 if (int.Parse(sr.ReadLine()) % 2 == 0)
  65.                 {
  66.                     ile++;
  67.                 }
  68.             }
  69.             sr.Close(); fs.Close();
  70.         }
  71.         static void PoliczLiczbyParzysteBinary(string plik)
  72.         {
  73.             FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
  74.             BinaryReader br = new BinaryReader(fs);
  75.             int ile = 0;
  76.             int poz = 0;
  77.             int help = 0;
  78.             int długość = (int)fs.Length;
  79.             while (poz < długość)
  80.             {
  81.                 help = br.ReadInt32();
  82.                 if (help % 2 == 0)
  83.                 {
  84.                     ile++;
  85.                 }
  86.                 poz += sizeof(int);
  87.             }
  88.  
  89.             br.Close(); fs.Close();
  90.            
  91.         }
  92.  
  93.         static long PobierzCzasParzTxt(string plik)
  94.         {
  95.             Stoper.Start();
  96.             PoliczLiczbyParzysteTxt(plik);
  97.             Stoper.Stop();
  98.             long res = Stoper.ElapsedMilliseconds;
  99.             Stoper.Reset();
  100.             return res;
  101.         }
  102.         static long PobierzCzasParzBinary(string plik)
  103.         {
  104.             Stoper.Start();
  105.             PoliczLiczbyParzysteBinary(plik);
  106.             Stoper.Stop();
  107.             long res = Stoper.ElapsedMilliseconds;
  108.             Stoper.Reset();
  109.             return res;
  110.         }
  111.  
  112.         static long PobierzWielkośćPliku(string plik)
  113.         {
  114.             FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
  115.             long res = fs.Length;
  116.             fs.Close();
  117.             return res;
  118.            
  119.         }
  120.         #endregion
  121.  
  122.         static void Main(string[] args)
  123.         {
  124.            
  125.             #region tablice z danymi
  126.             long[] TabDaneTxtStworzenie = new long[4];
  127.             long[] TabDaneBinaryStworzenie = new long[4];
  128.  
  129.             long[] TabDaneTxtParzyste = new long[4];
  130.             long[] TabDaneBinaryParzyste = new long[4];
  131.  
  132.             long[] TabDaneWielkPlikTxt = new long[4];
  133.             long[] TabDaneWielkPlikBinary = new long[4];
  134.             #endregion
  135.  
  136.             #region pobranie czasu zapisu do .txt
  137.             string złożenie = "";
  138.             string miliony = "000000";
  139.             int początek = 10;
  140.  
  141.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  142.             {
  143.                 złożenie = Convert.ToString(początek)+miliony;
  144.                 TabDaneTxtStworzenie[i] = PobierzCzasZapisuTxt(int.Parse(złożenie), Convert.ToString(początek) + "mln.txt");
  145.                 początek += 5;
  146.             }
  147.            
  148.                    
  149.             #endregion
  150.             #region pobranie czasu zapiu do .dat
  151.             miliony = "000000";
  152.             początek = 10;
  153.  
  154.             for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
  155.             {
  156.                 złożenie = Convert.ToString(początek) + miliony;
  157.                 TabDaneBinaryStworzenie[i] = PobierzCzasZapisuBinary(int.Parse(złożenie), Convert.ToString(początek) + "mln.dat");
  158.                 początek += 5;
  159.             }
  160.  
  161.             #endregion
  162.             #region pobranie wielkości plików
  163.  
  164.             początek = 10;
  165.             for (int i = 0; i < TabDaneWielkPlikTxt.Length; i++)
  166.             {
  167.                 TabDaneWielkPlikTxt[i] = PobierzWielkośćPliku(Convert.ToString(początek)+"mln.txt");
  168.                 TabDaneWielkPlikBinary[i] = PobierzWielkośćPliku(Convert.ToString(początek) + "mln.dat");
  169.                 początek += 5;
  170.             }
  171.            
  172.            
  173.             #endregion
  174.             #region pobranie czasu policzenia parzystych .txt        
  175.             miliony = "000000";
  176.             początek = 10;
  177.  
  178.      
  179.             for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
  180.             {
  181.                 złożenie = Convert.ToString(początek) + miliony;
  182.                 TabDaneTxtParzyste[i] = PobierzCzasParzTxt(Convert.ToString(początek) + "mln.txt");
  183.                 początek += 5;
  184.             }
  185.  
  186.             #endregion
  187.             #region pobranie czasu policzenia parzystych .dat
  188.             miliony = "000000";
  189.             początek = 10;
  190.  
  191.             for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
  192.             {
  193.                 złożenie = Convert.ToString(początek) + miliony;
  194.                 TabDaneBinaryParzyste[i] = PobierzCzasParzBinary(Convert.ToString(początek) + "mln.dat");
  195.                 początek += 5;
  196.             }
  197.  
  198.             #endregion
  199.  
  200.             #region zapis danych z tablicy do pliku
  201.             FileStream fs = new FileStream("wynik.txt", FileMode.Create, FileAccess.ReadWrite);
  202.             StreamWriter sw = new StreamWriter(fs);
  203.  
  204.             sw.WriteLine("Czasy stworzenia plików tektstowych");
  205.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  206.                 sw.WriteLine(TabDaneTxtStworzenie[i]);
  207.  
  208.             sw.WriteLine("Czasy stworzenia plików binarnych");
  209.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  210.                 sw.WriteLine(TabDaneBinaryStworzenie[i]);
  211.  
  212.             sw.WriteLine("Czasy policzenia liczb parzystych w tektstowych");
  213.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  214.                 sw.WriteLine(TabDaneTxtParzyste[i]);
  215.  
  216.             sw.WriteLine("Czasy policzenia liczb parzystych w binarnych");
  217.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  218.                 sw.WriteLine(TabDaneBinaryParzyste[i]);
  219.  
  220.             sw.WriteLine("Wielkości plików tekstowych");
  221.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  222.                 sw.WriteLine(TabDaneWielkPlikTxt[i]);
  223.  
  224.             sw.WriteLine("Wielkości plików binarnych");
  225.             for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
  226.                 sw.WriteLine(TabDaneWielkPlikBinary[i]);
  227.  
  228.             sw.Close();
  229.             #endregion
  230.  
  231.  
  232.            
  233.            
  234.         }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement