-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (56 loc) · 1.78 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
namespace MemoryProblem
{
class Product
{
public byte[] Image { get; set; }
public int Price { get; set; }
public Product[] SubProducts { get; set; }
}
class Program
{
static void Main(string[] args)
{
var productCache = new Queue<Product>();
// Fill cache with 100 products
for (var i = 0; i < 100; i++)
{
productCache.Enqueue(GenerateDemoProduct());
}
for (var i = 0; i < 1000; i++)
{
// Calculate the average price of products in cache
var averagePrice = productCache.Average(p => p.Price);
Console.WriteLine(averagePrice);
// Remove oldest 10 products, add 10 new products
for (var j = 0; j < 10; j++)
{
productCache.Dequeue();
productCache.Enqueue(GenerateDemoProduct());
}
}
}
private static int seed = 0;
public static Product GenerateDemoProduct()
{
var random = new Random(++seed);
var result = new Product
{
Image = new byte[random.Next(1024, 10240)],
Price = random.Next(10, 20)
};
if (random.Next(0, 3) == 0)
{
var numberOfSubProducts = random.Next(0, 6);
result.SubProducts = new Product[numberOfSubProducts];
for (var i = 0; i < numberOfSubProducts; i++)
{
result.SubProducts[i] = GenerateDemoProduct();
}
}
return result;
}
}
}