-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewExtractionBench.php
More file actions
122 lines (97 loc) · 3.25 KB
/
Copy pathViewExtractionBench.php
File metadata and controls
122 lines (97 loc) · 3.25 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray\Tests\Benchmark;
use PhpMlKit\NDArray\DType;
use PhpMlKit\NDArray\NDArray;
/**
* Benchmark tests for view extraction performance.
*/
class ViewExtractionBench
{
/**
* Run all benchmarks.
*/
public static function run(): void
{
echo "=== View Extraction Performance Benchmarks ===\n\n";
self::benchContiguousViewExtraction();
self::benchStridedViewExtraction();
self::benchSingleElementView();
self::benchLargeArrayViews();
}
/**
* Benchmark contiguous view extraction (should be fast).
*/
private static function benchContiguousViewExtraction(): void
{
echo "Benchmark: Contiguous View Extraction\n";
echo str_repeat('-', 50)."\n";
$sizes = [100, 1000];
foreach ($sizes as $size) {
$arr = NDArray::zeros([$size, $size], DType::Float64);
$start = microtime(true);
for ($i = 0; $i < 100; ++$i) {
$view = $arr->slice([':', ':']);
$result = $view->add(1.0);
}
$elapsed = (microtime(true) - $start) * 1000;
echo \sprintf(" Size %dx%d: %.3f ms (100 iterations)\n", $size, $size, $elapsed);
}
echo "\n";
}
/**
* Benchmark strided view extraction (may be slower).
*/
private static function benchStridedViewExtraction(): void
{
echo "Benchmark: Strided View Extraction\n";
echo str_repeat('-', 50)."\n";
$sizes = [100, 1000];
foreach ($sizes as $size) {
$arr = NDArray::zeros([$size, $size], DType::Float64);
$start = microtime(true);
for ($i = 0; $i < 100; ++$i) {
$view = $arr->slice(['::2', '::2']);
$result = $view->add(1.0);
}
$elapsed = (microtime(true) - $start) * 1000;
echo \sprintf(" Size %dx%d (step 2): %.3f ms (100 iterations)\n", $size, $size, $elapsed);
}
echo "\n";
}
/**
* Benchmark single element view (edge case).
*/
private static function benchSingleElementView(): void
{
echo "Benchmark: Single Element View\n";
echo str_repeat('-', 50)."\n";
$arr = NDArray::zeros([100, 100], DType::Float64);
$start = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
$view = $arr->slice(['50:51', '50:51']);
$result = $view->add(1.0);
}
$elapsed = (microtime(true) - $start) * 1000;
echo \sprintf(" Single element (1000 iterations): %.3f ms\n", $elapsed);
echo "\n";
}
/**
* Benchmark large array views.
*/
private static function benchLargeArrayViews(): void
{
echo "Benchmark: Large Array Views\n";
echo str_repeat('-', 50)."\n";
$size = 1000;
$arr = NDArray::zeros([$size, $size, 10], DType::Float64);
$start = microtime(true);
for ($i = 0; $i < 10; ++$i) {
$view = $arr->slice(['0:500', '0:500', ':']);
$result = $view->add(1.0);
}
$elapsed = (microtime(true) - $start) * 1000;
echo \sprintf(" 3D array slice (10 iterations): %.3f ms\n", $elapsed);
echo "\n";
}
}