Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Threading;
- using System.Diagnostics;
- namespace Zadanie_2
- {
- class Program
- {
- static Random los = new Random();
- static Stopwatch Stoper = new Stopwatch();
- #region metody
- static void GenerujLiczbyIZapiszTxt(int Ile,string plik)
- {
- FileStream fs = new FileStream(plik, FileMode.Create, FileAccess.ReadWrite);
- StreamWriter sw = new StreamWriter(fs);
- for (int i = 0; i < Ile; i++)
- sw.WriteLine (Convert.ToString(los.Next(int.MinValue, int.MaxValue)));
- sw.Close();
- fs.Close();
- }
- static void GenerujLiczbyIZapiszBinary(int Ile,string plik)
- {
- FileStream fs = new FileStream(plik, FileMode.Create, FileAccess.ReadWrite);
- BinaryWriter bw = new BinaryWriter(fs);
- for (int i = 0; i < Ile; i++)
- bw.Write(los.Next(int.MinValue, int.MaxValue));
- bw.Close();
- fs.Close();
- }
- static long PobierzCzasZapisuTxt(int input, string plik)
- {
- Stoper.Start();
- GenerujLiczbyIZapiszTxt(input, plik);
- Stoper.Stop();
- long res = Stoper.ElapsedMilliseconds;
- Stoper.Reset();
- return res;
- }
- static long PobierzCzasZapisuBinary(int input,string plik)
- {
- Stoper.Start();
- GenerujLiczbyIZapiszBinary(input, plik);
- Stoper.Stop();
- long res = Stoper.ElapsedMilliseconds;
- Stoper.Reset();
- return res;
- }
- static void PoliczLiczbyParzysteTxt(string plik)
- {
- FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
- StreamReader sr = new StreamReader(fs);
- int ile = 0;
- while (!sr.EndOfStream)
- {
- if (int.Parse(sr.ReadLine()) % 2 == 0)
- {
- ile++;
- }
- }
- sr.Close(); fs.Close();
- }
- static void PoliczLiczbyParzysteBinary(string plik)
- {
- FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
- BinaryReader br = new BinaryReader(fs);
- int ile = 0;
- int poz = 0;
- int help = 0;
- int długość = (int)fs.Length;
- while (poz < długość)
- {
- help = br.ReadInt32();
- if (help % 2 == 0)
- {
- ile++;
- }
- poz += sizeof(int);
- }
- br.Close(); fs.Close();
- }
- static long PobierzCzasParzTxt(string plik)
- {
- Stoper.Start();
- PoliczLiczbyParzysteTxt(plik);
- Stoper.Stop();
- long res = Stoper.ElapsedMilliseconds;
- Stoper.Reset();
- return res;
- }
- static long PobierzCzasParzBinary(string plik)
- {
- Stoper.Start();
- PoliczLiczbyParzysteBinary(plik);
- Stoper.Stop();
- long res = Stoper.ElapsedMilliseconds;
- Stoper.Reset();
- return res;
- }
- static long PobierzWielkośćPliku(string plik)
- {
- FileStream fs = new FileStream(plik, FileMode.Open, FileAccess.ReadWrite);
- long res = fs.Length;
- fs.Close();
- return res;
- }
- #endregion
- static void Main(string[] args)
- {
- #region tablice z danymi
- long[] TabDaneTxtStworzenie = new long[4];
- long[] TabDaneBinaryStworzenie = new long[4];
- long[] TabDaneTxtParzyste = new long[4];
- long[] TabDaneBinaryParzyste = new long[4];
- long[] TabDaneWielkPlikTxt = new long[4];
- long[] TabDaneWielkPlikBinary = new long[4];
- #endregion
- #region pobranie czasu zapisu do .txt
- string złożenie = "";
- string miliony = "000000";
- int początek = 10;
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- {
- złożenie = Convert.ToString(początek)+miliony;
- TabDaneTxtStworzenie[i] = PobierzCzasZapisuTxt(int.Parse(złożenie), Convert.ToString(początek) + "mln.txt");
- początek += 5;
- }
- #endregion
- #region pobranie czasu zapiu do .dat
- miliony = "000000";
- początek = 10;
- for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
- {
- złożenie = Convert.ToString(początek) + miliony;
- TabDaneBinaryStworzenie[i] = PobierzCzasZapisuBinary(int.Parse(złożenie), Convert.ToString(początek) + "mln.dat");
- początek += 5;
- }
- #endregion
- #region pobranie wielkości plików
- początek = 10;
- for (int i = 0; i < TabDaneWielkPlikTxt.Length; i++)
- {
- TabDaneWielkPlikTxt[i] = PobierzWielkośćPliku(Convert.ToString(początek)+"mln.txt");
- TabDaneWielkPlikBinary[i] = PobierzWielkośćPliku(Convert.ToString(początek) + "mln.dat");
- początek += 5;
- }
- #endregion
- #region pobranie czasu policzenia parzystych .txt
- miliony = "000000";
- początek = 10;
- for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
- {
- złożenie = Convert.ToString(początek) + miliony;
- TabDaneTxtParzyste[i] = PobierzCzasParzTxt(Convert.ToString(początek) + "mln.txt");
- początek += 5;
- }
- #endregion
- #region pobranie czasu policzenia parzystych .dat
- miliony = "000000";
- początek = 10;
- for (int i = 0; i < TabDaneBinaryStworzenie.Length; i++)
- {
- złożenie = Convert.ToString(początek) + miliony;
- TabDaneBinaryParzyste[i] = PobierzCzasParzBinary(Convert.ToString(początek) + "mln.dat");
- początek += 5;
- }
- #endregion
- #region zapis danych z tablicy do pliku
- FileStream fs = new FileStream("wynik.txt", FileMode.Create, FileAccess.ReadWrite);
- StreamWriter sw = new StreamWriter(fs);
- sw.WriteLine("Czasy stworzenia plików tektstowych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneTxtStworzenie[i]);
- sw.WriteLine("Czasy stworzenia plików binarnych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneBinaryStworzenie[i]);
- sw.WriteLine("Czasy policzenia liczb parzystych w tektstowych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneTxtParzyste[i]);
- sw.WriteLine("Czasy policzenia liczb parzystych w binarnych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneBinaryParzyste[i]);
- sw.WriteLine("Wielkości plików tekstowych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneWielkPlikTxt[i]);
- sw.WriteLine("Wielkości plików binarnych");
- for (int i = 0; i < TabDaneTxtStworzenie.Length; i++)
- sw.WriteLine(TabDaneWielkPlikBinary[i]);
- sw.Close();
- #endregion
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement