VladislavSavvateev

Untitled

Nov 24th, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.64 KB | None | 0 0
  1. using System.Diagnostics;
  2.  
  3. public enum TimeFrame {
  4.     Year, TwoYears, Long
  5. }
  6.  
  7. public class Paper {
  8.     public string Name { get; set; }
  9.     public Person Author { get; set; }
  10.     public DateTime PublicationDate { get; set; }
  11.    
  12.     public Paper(string name, Person author, DateTime publicationDate) {
  13.         Name = name;
  14.         Author = author;
  15.         PublicationDate = publicationDate;
  16.     }
  17.  
  18.     public Paper() {
  19.         Name = "Paper Name";
  20.         Author = new Person("", "");
  21.         PublicationDate = DateTime.Now;
  22.     }
  23.  
  24.     public override string ToString() {
  25.         return $"Paper \"{Name}\" by {Author}, published at {PublicationDate:yyyy-MM-dd HH:mm}";
  26.     }
  27. }
  28.  
  29. public class ResearchTeam {
  30.     private string _topic;
  31.     private string _organization;
  32.     private int _registrationNumber;
  33.     private TimeFrame _researchTimeFrame;
  34.     private Paper[] _papers;
  35.  
  36.     public Paper? LastPaper {
  37.         get {
  38.             return _papers.MaxBy(p => p.PublicationDate);
  39.         }
  40.     }
  41.     public bool this[TimeFrame timeFrame] {
  42.         get {
  43.             return _researchTimeFrame == timeFrame;
  44.         }
  45.     }
  46.  
  47.     public string Topic {
  48.         get {
  49.             return _topic;
  50.         }
  51.     }
  52.     public string Organization {
  53.         get {
  54.             return _organization;
  55.         }
  56.     }
  57.     public int RegistrationNumber {
  58.         get {
  59.             return _registrationNumber;
  60.         }
  61.     }
  62.     public TimeFrame ResearchTimeFrame {
  63.         get {
  64.             return _researchTimeFrame;
  65.         }
  66.     }
  67.     public Paper[] Papers {
  68.         get {
  69.             return _papers;
  70.         }
  71.     }
  72.  
  73.     public ResearchTeam(string topic, string organization, int registrationNumber, TimeFrame researchTimeFrame) {
  74.         _topic = topic;
  75.         _organization = organization;
  76.         _registrationNumber = registrationNumber;
  77.         _researchTimeFrame = researchTimeFrame;
  78.         _papers = Array.Empty<Paper>();
  79.     }
  80.    
  81.     public ResearchTeam() {
  82.         _topic = "Topic";
  83.         _organization = "Organization";
  84.         _registrationNumber = 0;
  85.         _researchTimeFrame = TimeFrame.Year;
  86.         _papers = Array.Empty<Paper>();
  87.     }
  88.  
  89.     public void AddPapers(params Paper[] papers) {
  90.         _papers = _papers.Concat(papers).ToArray();
  91.     }
  92.  
  93.     public override string ToString() {
  94.             return $"Research team \"{_topic}\" ({_organization}, {_registrationNumber}), " +
  95.                    $"{_researchTimeFrame} time frame, {_papers.Length} papers: " +
  96.                    $"{string.Join(", ", _papers.AsEnumerable())}";
  97.     }
  98.     public string ToShortString() {
  99.         return $"Research team \"{_topic}\" ({_organization}, {_registrationNumber}), " +
  100.                $"{_researchTimeFrame} time frame, {_papers.Length} papers";
  101.     }
  102. }
  103.  
  104. public class Person {
  105.     public string FirstName { get; }
  106.     public string LastName { get; }
  107.    
  108.     public Person(string firstName, string lastName) {
  109.         FirstName = firstName;
  110.         LastName = lastName;
  111.     }
  112.  
  113.     public override string ToString() { return $"{FirstName} {LastName}"; }
  114. }
  115.  
  116. class Program {
  117.     public static void Main(string[] args) {
  118.         var researchTeam = new ResearchTeam();
  119.         Console.WriteLine("ResearchTeam.ToShortString(): {0}", researchTeam.ToShortString());
  120.  
  121.         foreach (var enumValue in Enum.GetValues<TimeFrame>()) {
  122.             Console.WriteLine("ResearchTeam[{0}]: {1}", enumValue, researchTeam[enumValue]);
  123.         }
  124.        
  125.         Console.WriteLine("===============================================================");
  126.        
  127.         researchTeam = new ResearchTeam("влияние мэшапов на стул по утрам",
  128.             "ООО \"Разработка очка\"", 123, TimeFrame.Year);
  129.        
  130.         Console.WriteLine("ResearchTeam.ToShortString(): {0}", researchTeam);
  131.        
  132.         researchTeam.AddPapers(new Paper {
  133.             Name = "напас лавандос вместо лоперамида",
  134.             Author = new Person("хуй", "моржовый"),
  135.             PublicationDate = new DateTime(2022, 5, 9, 12, 0, 0)
  136.         }, new Paper {
  137.             Name = "производство мэшапов из лоперамида",
  138.             Author = new Person("кто", "такой"),
  139.             PublicationDate = new DateTime(2021, 12, 1, 5, 0, 0)
  140.         }, new Paper {
  141.             Name = "мэшапы с меладзе как слабительное",
  142.             Author = new Person("пиздец", "блядский"),
  143.             PublicationDate = new DateTime(2021, 7, 15, 18, 0, 0)
  144.         }, new Paper {
  145.             Name = "мэшапы с новогодним поздравлением от путина: с новым мэшапом, пидоры!",
  146.             Author = new Person("путин", "владимир"),
  147.             PublicationDate = new DateTime(2021, 12, 31, 12, 0, 0)
  148.         });
  149.        
  150.         Console.WriteLine("ResearchTeam.LastPaper: {0}", researchTeam.LastPaper);
  151.        
  152.         Console.WriteLine("===============================================================");
  153.  
  154.         var array1 = new Paper[10000];
  155.         var array2 = new Paper[100][];
  156.         var array3 = new Paper[100][];
  157.        
  158.         // init array1
  159.         for (var i = 0; i < array1.Length; i++) {
  160.             array1[i] = new Paper {
  161.                 Name = "paper" + i,
  162.                 Author = new Person("author" + i, "author" + i),
  163.                 PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  164.             };
  165.         }
  166.        
  167.         // init array2
  168.         for (var i = 0; i < array2.Length; i++) {
  169.             var arr = new Paper[100];
  170.             for (var j = 0; j < arr.Length; j++) {
  171.                 arr[j] = new Paper {
  172.                     Name = "paper" + i + j,
  173.                     Author = new Person("author" + i + j, "author" + i + j),
  174.                     PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  175.                 };
  176.             }
  177.             array2[i] = arr;
  178.         }
  179.        
  180.         // init array3
  181.         var random = new Random();
  182.         for (var i = 0; i < array3.Length; i++) {
  183.             var arr = new Paper[random.Next(50, 150)];
  184.             for (var j = 0; j < arr.Length; j++) {
  185.                 arr[j] = new Paper {
  186.                     Name = "paper" + i + j,
  187.                     Author = new Person("author" + i + j, "author" + i + j),
  188.                     PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  189.                 };
  190.             }
  191.             array3[i] = arr;
  192.         }
  193.        
  194.         // test access to array1
  195.         var sw = new Stopwatch();
  196.         sw.Start();
  197.        
  198.         for (var i = 0; i < 10000000; i++) {
  199.             var paper = array1[random.Next(0, array1.Length)];
  200.             DoSomething(paper);
  201.         }
  202.        
  203.         sw.Stop();
  204.         Console.WriteLine("array1|access: {0}ms", sw.ElapsedMilliseconds);
  205.        
  206.         // test access to array2
  207.         sw.Restart();
  208.        
  209.         for (var i = 0; i < 10000000; i++) {
  210.             var paper = array2[random.Next(0, array2.Length)][random.Next(0, array2.Length)];
  211.             DoSomething(paper);
  212.         }
  213.        
  214.         sw.Stop();
  215.         Console.WriteLine("array2|access: {0}ms", sw.ElapsedMilliseconds);
  216.        
  217.         // test access to array3
  218.         sw.Restart();
  219.        
  220.         for (var i = 0; i < 10000000; i++) {
  221.             var arr = array3[random.Next(0, array3.Length)];
  222.             var paper = arr[random.Next(0, arr.Length)];
  223.             DoSomething(paper);
  224.         }
  225.        
  226.         sw.Stop();
  227.         Console.WriteLine("array3|access: {0}ms", sw.ElapsedMilliseconds);
  228.        
  229.         // test write to array1
  230.         sw.Restart();
  231.        
  232.         for (var i = 0; i < 10000000; i++) {
  233.             array1[random.Next(0, array1.Length)] = new Paper {
  234.                 Name = "paper" + i,
  235.                 Author = new Person("author" + i, "author" + i),
  236.                 PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  237.             };
  238.         }
  239.        
  240.         sw.Stop();
  241.         Console.WriteLine("array1|write: {0}ms", sw.ElapsedMilliseconds);
  242.        
  243.         // test write to array2
  244.         sw.Restart();
  245.        
  246.         for (var i = 0; i < 10000000; i++) {
  247.             array2[random.Next(0, array2.Length)][random.Next(0, array2.Length)] = new Paper {
  248.                 Name = "paper" + i,
  249.                 Author = new Person("author" + i, "author" + i),
  250.                 PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  251.             };
  252.         }
  253.        
  254.         sw.Stop();
  255.         Console.WriteLine("array2|write: {0}ms", sw.ElapsedMilliseconds);
  256.        
  257.         // test write to array3
  258.         sw.Restart();
  259.        
  260.         for (var i = 0; i < 10000000; i++) {
  261.             var arr = array3[random.Next(0, array3.Length)];
  262.             arr[random.Next(0, arr.Length)] = new Paper {
  263.                 Name = "paper" + i,
  264.                 Author = new Person("author" + i, "author" + i),
  265.                 PublicationDate = new DateTime(2021, 1, 1, 0, 0, 0)
  266.             };
  267.         }
  268.        
  269.         sw.Stop();
  270.         Console.WriteLine("array3|write: {0}ms", sw.ElapsedMilliseconds);
  271.     }
  272.  
  273.     private static void DoSomething(Paper paper) {
  274.        
  275.     }
  276. }
  277.  
Add Comment
Please, Sign In to add comment