-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution7.cs
More file actions
49 lines (40 loc) · 1.45 KB
/
Solution7.cs
File metadata and controls
49 lines (40 loc) · 1.45 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Buffers;
using System.Linq;
namespace cSharpSolutions
{
internal class Solution7
{
public static double[] ProcessArrayStandard(double[] input)
{
double[] result = new double[input.Length];
for (int i = 0; i < input.Length; i++)
result[i] = input[i] * 2; // Example processing
return result;
}
public static double[] ProcessArrayOptimized(double[] input)
{
var pool = ArrayPool<double>.Shared;
double[] result = pool.Rent(input.Length);
for (int i = 0; i < input.Length; i++)
result[i] = input[i] * 2;
double[] finalResult = result.Take(input.Length).ToArray();
pool.Return(result);
return finalResult;
}
public static void Run()
{
Console.WriteLine("=== Solution 7: ArrayPool Memory Optimization ===");
double[] arr = Enumerable.Range(1, 10).Select(x => (double)x).ToArray();
var standard = ProcessArrayStandard(arr);
var optimized = ProcessArrayOptimized(arr);
Console.WriteLine("Standard: " + string.Join(", ", standard));
Console.WriteLine("Optimized: " + string.Join(", ", optimized));
Console.WriteLine("\nSolution 7 complete");
}
}
}